mkamthan
mkamthan

Reputation: 2371

upload a file to a MySql DB with PHP

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.

Upvotes: 0

Views: 2055

Answers (3)

Pascal MARTIN
Pascal MARTIN

Reputation: 400932

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?

Upvotes: 4

stefita
stefita

Reputation: 1793

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

Upvotes: 1

Quassnoi
Quassnoi

Reputation: 425261

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.

Upvotes: 1

Related Questions