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?

Vic Seedoubleyew
  • 9,888
  • 6
  • 55
  • 76
  • 1
    This error means, the file is not present in the folder you have specified. – Niranjan N Raju Nov 08 '15 at 05: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 http://php.net/manual/en/function.move-uploaded-file.php – rjdown Nov 08 '15 at 05:33

1 Answers1

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.

Community
  • 1
  • 1
Ultimater
  • 4,647
  • 2
  • 29
  • 43
  • I would not say it's bad practice to store files in DB it really depends. – Robert Nov 08 '15 at 06:59
  • 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... – Ultimater Nov 08 '15 at 07:56
  • 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? – Kartikay Anand Nov 09 '15 at 07:11
  • I'd recommend playing around with https://blueimp.github.io/jQuery-File-Upload/ The source code for that is here: https://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: https://github.com/blueimp/jQuery-File-Upload/wiki/PHP-MySQL-database-integration – Ultimater Nov 09 '15 at 07:45
  • 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: http://stackoverflow.com/questions/12276188/should-bmp-files-be-used-for-websites Then that leaves image formats like TIF[F] which also aren't meant for the web. – Ultimater Nov 09 '15 at 07:52