I want the users to upload files through my webapp I am developing in PHP usinig MySql in the backend. I want to store the files in the database. I am facing problems in doi
The file you upload goes to the upload directory.
You can get the name of the file by looking into $_FILES['binFile']['tmp_name']
You should do something with the file before the script completes, otherwise the web server will delete it.
You should read the contents of the file and put them into your BLOB
column.
You might want to take a look at the upload section of the PHP manual : Handling file uploads ; it would probably be a good start ;-)
For instance, you might see that the file's informations are store in $_FILES
, and not in $_POST
(see POST method uploads) -- at least, considering your example, I suppose you are searching for the file in $_POST
, and not $_FILES
.
in your case, considering the input field is named "binFile
", you'd probably want to use var_dump (or any equivalent) on $_FILEs['binFile']
, to see what's inside ;-)
Then, you can use is_uploaded_file and move_uploaded_file to work with the file itself.
Then, are you sure you want to store the file's content into the Database, and not on disk, only storing into DB the path to the file ?
About that, you can take a look at this question and its answers : Storing Images in DB - Yea or Nay? -- it's not specific to PHP, but the ideas should still be true.
Maybe Where to store uploaded files (sound, pictures and video) could help too ;-)
Same about Storing a small number of images: blob or fs?, and/or Store pictures as files or in the database for a web app?
it's in the tmp directory, till you move it
$userfile = $_FILES['binFil']['tmp_name'];
move_uploaded_file($userfile , "somedir");
Afterwords you should detect what type it is with something like
$userfileExt = array_pop(explode(':', $userfile));