本文介绍了更好的方式来存储大型文件在MySQL数据库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个PHP脚本,可以上传非常大的文件(高达500MB),文件内容存储在MySQL数据库中。目前我做的是这样的:

pre $ mysql_query(INSERT INTO TABLE VALUES(''));

$ uploadedfile = fopen($ _ FILES ['file'] ['tmp_name'],'rb');
while(!feof($ uploadedfile)){
$ line = mysql_escape_string(fgets($ uploadedfile,4096));
mysql_query(UPDATE table SET file = CONCAT(file,'$ line')WHERE something = something);
}
fclose($ uploadedfile);

这当然会遇到大量的sql查询。



我这样做,而不是像$ / b>

$ $ $ file = file_get_contents tmp_name的值']);
mysql_query(INSERT INTO table VALUES('$ file'));

因为无论使用多少内存,比使用500MB的内存。

然而,一定有更好的办法。我应该继续做file_get_contents的方式,还是有比CONCAT更好的方法,或者是我现在做的方式是所有的邪恶较小?

解决方案

我总是将我的文件存储在服务器上,并将它们的位置存储在数据库中。


I have a PHP script that you can upload very large files with (up to 500MB), and the file's content is stored in a MySQL database. Currently I do something like this:

mysql_query("INSERT INTO table VALUES('')");

$uploadedfile = fopen($_FILES['file']['tmp_name'], 'rb');
while (!feof($uploadedfile)) {
    $line = mysql_escape_string(fgets($uploadedfile, 4096));
    mysql_query("UPDATE table SET file = CONCAT(file, '$line') WHERE something = something");
}
fclose($uploadedfile);

This of course does a bloody lot of sql queries.

I did that rather than something like

$file = file_get_contents($_FILES['file']['tmp_name']);
mysql_query("INSERT INTO table VALUES('$file')");

because that would use up however much memory the file was, and it seemed better to do more sql queries than to use 500 mb of memory.
However, there must be a better way. Should I go ahead and do it the file_get_contents way or is there a better way than CONCAT, or is the way I'm doing it now the lesser of all evils?

解决方案

I always store my files on the server, and store their location in the database.

这篇关于更好的方式来存储大型文件在MySQL数据库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-14 22:38