当文件太大时,PHP 添加斜杠失败


PHP addslashes failing when files too large

我有一组PHP脚本,可以将文件加载到数据库中,供自动更新程序稍后使用。 该程序运行正常,直到文件超过10MB范围。 该脚本的粗略思路是,它从特定位置的磁盘中提取文件,并将它们加载到数据库中。 这允许我们存储在源代码管理中,并根据需要在集合中更新。

最初,我认为根据我的初始搜索,我达到了数据库SQL的限制。 但是,经过进一步的测试,它似乎是特定于PHP的。 我检查了 Apache 错误日志,但没有看到此脚本或包含的任何错误。一旦 PHP 脚本到达 addslashes 函数,脚本似乎就会停止执行。 (我在每个脚本语句之间添加了 echo 语句。

我希望这是我缺少的简单东西,但是在网上搜索几个小时后,我找不到与添加斜杠失败相关的任何内容。

有什么想法吗?

提前谢谢。

mysql_connect('localhost', '****', '****') or die('Could not connect to the database');
mysql_select_db('****') or die('Could not select database');
function get_filelist($path)
{
        return get_filelist_recursive("/build/".$path);
}
function get_filelist_recursive($path)
{
        $i = 0;
        $list = array();
        if( !is_dir($path) )
                return get_filedetails($path);
        if ($handle = opendir($path))
        {
                while (false !== ($file = readdir($handle)))
                {
                        if($file!='.' && $file!='..' && $file[0]!='.')
                        {
                                if( is_dir($path.'/'.$file) )
                                {
                                        $list = $list + get_filelist_recursive($path.'/'.$file);
                                }
                                else
                                {
                                        $list = $list + get_filedetails($path.'/'.$file);
                                }
                        }
                }
                closedir($handle);
                return $list;
        }
}
function get_filedetails($path)
{
        $item = array();
        $details = array();
        $details[0] = filesize($path);
        $details[1] = sha1_file($path);
        $item[$path] = $details;
        return $item;
}
$productset = mysql_query("select * from product where status is null and id=".$_REQUEST['pid']);
$prow = mysql_fetch_assoc($productset);
$folder = "product/".$prow['name'];
$fileset = get_filelist($folder);
while (list($key, $val) = each($fileset))
{
    $fh = fopen($key, 'rb') or die("Cannot open file");
    $data = fread($fh,$val[0]);
    $data = addslashes($data);
    fclose($fh);
    $filename = substr( $key, strlen($folder) + 1 );
    $query = "insert into file(name,size,hash,data,manifest_id) values('".$filename."','".$val[0]."','".$val[1]."','".$data."','".$prow['manifest_id']."')";
    $retins = mysql_query($query);
    if( $retins == false )
        echo "BUILD FAILED: $key, $val[0] $val[1].<br>'n";
}
header("Location: /patch/index.php?pid=".$_REQUEST['pid']);

不要使用添加斜杠,在这种情况下使用 mysql_real_escape_string。 此外,尝试插入如此大的文件可能会达到max_allowed_packet限制。 默认值为 1MB。

如果您使用 mysqli(推荐),您可以指示该列是二进制的,它将以块的形式发送查询。

还要确保你没有达到任何PHP内存限制或最大执行时间。