-1

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

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

我希望这是我缺少的一些简单的东西,但是经过几个小时的在线搜索后,我找不到与 addlashes 失败相关的任何内容。

有任何想法吗?

提前致谢。

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']);
4

1 回答 1

1

不要使用addlashes,mysql_real_escape_string在这种情况下使用。此外,您可能会通过尝试插入如此大的文件来达到max_allowed_pa​​cket限制。默认值为 1MB。

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

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

于 2012-06-10T07:48:07.400 回答