0

HTML code:

<input type="file" name="files[]" multiple="multiple" id="photo" >

PHP code:

$query1="insert into imgtab values('".$ka."','".addslashes(file_get_contents($_FILES['files']['name'][$i]))."')";

Error:

Warning: file_get_contents(Screenshot (5).png): failed to open stream: No such file or directory in C:\xampp\htdocs\q.php on line 16

How can I solve this error?

2
  • 1
    This error means, the file is not present in the folder you have specified. Commented Nov 8, 2015 at 5:27
  • 1
    $_FILES['files']['name'] refers to the name of the file before it was uploaded. You really need to use move_uploaded_file before you start storing anything in the database. Have a look here php.net/manual/en/function.move-uploaded-file.php Commented Nov 8, 2015 at 5:33

1 Answer 1

0

Try using file_get_contents($_FILES['files']['tmp_name'][$i]) instead since ['name'] is a reference to the uploaded file name, not the file location. It is also bad practice to store files in the database. So you'd best use move_uploaded_file(...) then store the location to your database rather than the actual file contents.

Sign up to request clarification or add additional context in comments.

5 Comments

I would not say it's bad practice to store files in DB it really depends.
There are several drawbacks with storing it in the database. The database will become huge and quickly hit your limit. Access to your files now requires going through a DB layer thus more bottleneck. Unable to serve the file from a different server. If your file is bigger than your data type, the file contents will get truncated. With a file system there's a chance the file will survive in the RAM unlike SELECT which will always read from the disk. DB backups/restore will take much longer and require a lot more disk space, especially if you keep multiple backups around. I could go on...
thanx sir tmp_name really worked..but i m new to storing images in a folder on server..could u provide me witha link to any of it's tutorial?
I'd recommend playing around with blueimp.github.io/jQuery-File-Upload The source code for that is here: github.com/blueimp/jQuery-File-Upload For the backend, there's a /server/php directory. All backend requests go through index.php which includes their UploadHandler.php file which is defined in such a way that you don't need to edit but you can extend it with your own class. Although the code by default doesn't work with a database, there's integration code already built for you here: github.com/blueimp/jQuery-File-Upload/wiki/…
A note of caution with the code though, you'll most likely want to generate a hash of the file contents rather than use the file names users upload to avoid issues with long names and spaces etc. Also you'll probably want to customized the allowed upload extensions and limit it to PNG, GIF, and JP[E]G. BMP for example isn't meant for the web: stackoverflow.com/questions/12276188/… Then that leaves image formats like TIF[F] which also aren't meant for the web.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.