如何使用PHP在MySQL数据库中插入大文件? [英] How can I insert large files in MySQL db using PHP?

查看:95
本文介绍了如何使用PHP在MySQL数据库中插入大文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将最大大小为10MB的大文件上传到我的MySQL数据库.使用.htaccess,我将PHP自己的文件上传限制更改为"10485760" = 10MB.我最多可以上传10MB的文件.

I want to upload a large file of maximum size 10MB to my MySQL database. Using .htaccess I changed PHP's own file upload limit to "10485760" = 10MB. I am able to upload files up to 10MB without any problem.

但是,如果文件大小超过1 MB,则无法将其插入数据库中.

But I can not insert the file in the database if it is more that 1 MB in size.

我正在使用file_get_contents读取所有文件数据,并将其作为要插入到LONGBLOB字段中的字符串传递给插入查询.

I am using file_get_contents to read all file data and pass it to the insert query as a string to be inserted into a LONGBLOB field.

但是,虽然我可以使用print_r($_FILES)来确保正确上传了文件,但是没有将大于1 MB的文件添加到数据库中.任何帮助将不胜感激,我将在接下来的6小时内需要它.所以,请帮忙!

But files bigger than 1 MB are not added to the database, although I can use print_r($_FILES) to make sure that the file is uploaded correctly. Any help will be appreciated and I will need it within the next 6 hours. So, please help!

推荐答案

据我所知,通常不将文件存储在db中是一种更快,更好的做法,因为它会很快变得庞大并使其变慢.最好的办法是将文件存储在目录中,然后仅将文件的位置存储在数据库中.

As far as I know it's generally quicker and better practice not to store the file in the db as it will get massive very quickly and slow it down. It's best to make a way of storing the file in a directory and then just store the location of the file in the db.

我们为工作中的CMS中的images/pdfs/mpegs等执行此操作,方法是为使用url安全文件名命名的文件创建一个文件夹,并将该文件夹名称存储在db中.然后,只需在表示层中将其网址写出即可.

We do it for images/pdfs/mpegs etc in the CMS we have at work by creating a folder for the file named from the url-safe filename and storing the folder name in the db. It's easy just to write out the url of it in the presentation layer then.

这篇关于如何使用PHP在MySQL数据库中插入大文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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