用PHP将文件上传到MySql数据库 [英] upload a file to a MySql DB with PHP

查看:675
本文介绍了用PHP将文件上传到MySql数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望用户通过我的web应用程序上传文件,我正在使用PHP在后端开发MySql。
我想将这些文件存储在数据库中。我在这样做时遇到问题。
此外,一旦文件存储在数据库中,我们如何去下载它,在webapp(文件类型和文件的其他属性)中正确显示它。


$
$ b $ p $ lt; code>< FORM METHOD =postACTION =fileUpload.phpENCTYPE = multipart / form-data>

< INPUT TYPE =hiddenNAME =MAX_FILE_SIZEVALUE =3000000>

< INPUT TYPE =hidden NAME =actionVALUE =upload>

< TABLE BORDER =1>
< TR>
< TD>说明:< / TD>
< TD>< TEXTAREA NAME =txtDescriptionROWS =10COLS =50> < / TEXTAREA>< / TD>
< / TR>
< TR>
< TD>档案:< / TD>
< TD>< INPUT TYPE =fileNAME =binFile>< / TD>
< / TR>
< TR>
< TD COLSPAN =2>< INPUT TYPE =submitNAME =UploadVALUE =Upload>< / TD>
< / TR>
< / TABLE>
< / FORM>



但是当我提交它时,我打印出$ _POST数组:
数组([MAX_FILE_SIZE] => 3000000 [action] => upload [txtDescription] => jassfhjabsf [上传] =>上传)文件内容binFile正在迷失。
任何人都可以请我帮忙吗?

问候,
Mayank。

解决方案

您可能需要查看PHP手册的上传部分:处理文件上传;这可能是一个很好的开始;)



例如,您可能会看到文件的信息存储在 $ _ FILES ,而不是 $ _ POST (参见 POST方法上传) - 至少,考虑你的例子,我想你正在搜索 $ _ POST 而不是 $ _ FILES



在您的情况下,考虑到输入字段名为 binFile ,你可能要使用 var_dump code> (或任何等价物)放在 $ _ FILEs ['binFile'] ,看看里面有什么;-)

然后,您可以使用 is_uploaded_file move_uploaded_file 以窝rk文件本身。



然后,您确定要将文件的内容存储到数据库中,而不是磁盘上存储到数据库的路径文件?

关于这一点,你可以看看这个问题和它的答案:

也许在哪里存储上传的文件(声音,图片和视频)也可以提供帮助;-)

同样的存储少量的图像:blob或fs?,和/或存储图片作为文件或在dat基于Web应用程序?


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.

解决方案

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?

这篇关于用PHP将文件上传到MySql数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆