views:

228

answers:

3

Hi!

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.

+4  A: 

Hi,

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?

Pascal MARTIN
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.
mkamthan
Oh, OK. Thanks for the explanation!
Pascal MARTIN
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.
mkamthan
Hi, I see you had an answer to that problem before I saw your message here (http://stackoverflow.com/questions/1316674/upload-size-problem-in-php-and-mysql), so I won't be answering here ^^ ;; still, have fun !
Pascal MARTIN
+1  A: 

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.

Quassnoi
+1  A: 

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