0

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 doing so. Also, once the file is stored in a Database how do we go for downloading it, displaying it correctly in the webapp (the file type, and other attributes of the file).

I use a form like:

<FORM METHOD="post" ACTION="fileUpload.php" ENCTYPE="multipart/form-data">
<INPUT TYPE="hidden" NAME="MAX_FILE_SIZE" VALUE="3000000">
<INPUT TYPE="hidden" NAME="action" VALUE="upload">
<TABLE BORDER="1"> <TR> <TD>Description: </TD> <TD><TEXTAREA NAME="txtDescription" ROWS="10" COLS="50"> </TEXTAREA></TD> </TR> <TR> <TD>File: </TD> <TD><INPUT TYPE="file" NAME="binFile"></TD> </TR> <TR> <TD COLSPAN="2"><INPUT TYPE="submit" NAME="Upload" VALUE="Upload"></TD> </TR> </TABLE> </FORM>

but when i submit it and i print out the $_POST array i get : Array ( [MAX_FILE_SIZE] => 3000000 [action] => upload [txtDescription] => jassfhjabsf [Upload] => Upload )

I am unable to understand where the file content "binFile" is getting lost. Can anybody please help me?

Regards, Mayank.

3 Answers 3

4

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?

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

3 Comments

Thanks a lot Pascal! :) Yes, I am sure about it that i want to store in the DB and not in FS as I have to maintain mapping certain mappings between users and the files uploaded by them w.r.t. a parameter. Maintaining directory structure will be tough and tedious while DB solves my problem itself.
Hi Pascal, I am now able to insert files in the DB, but files > 1MB in size are not getting inserted in the DB. I am using a LONGBLOB as the data type of the column in which I save the file. Can you please help me out? Regards, Mayank.
Hi, I see you had an answer to that problem before I saw your message here (stackoverflow.com/questions/1316674/…), so I won't be answering here ^^ ;; still, have fun !
1

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.

Comments

1

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));

Comments

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.