Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/234.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/google-sheets/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
当文件太大时,PHP addslashes会失败_Php - Fatal编程技术网

当文件太大时,PHP addslashes会失败

当文件太大时,PHP addslashes会失败,php,Php,我有一组PHP脚本,可以将文件加载到数据库中,供自动更新程序稍后使用。程序工作正常,直到文件超过10MB的范围。该脚本的大致思想是,它从特定位置的磁盘中提取文件,并将其加载到数据库中。这允许我们存储在源代码管理中,并根据需要进行集合更新 最初,我认为基于最初的搜索,我已经达到了数据库SQL的极限。然而,在进一步测试之后,它似乎是PHP特有的。我检查了Apache错误日志,但没有看到此脚本或includes的任何错误。一旦PHP脚本到达addslashes函数,脚本似乎停止执行。(我在每个脚本语句

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

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

我希望这是我遗漏的一些简单的东西,但经过几个小时的在线搜索,我找不到任何与addslashes失败相关的东西

有什么想法吗

提前谢谢

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_connect('localhost'、'**'、'**')或die('cannotconnecttothedatabase');
mysql_select_db('**')或die('not select database');
函数get\u filelist($path)
{
返回get\u filelist\u recursive(“/build/”$path);
}
函数get\u filelist\u recursive($path)
{
$i=0;
$list=array();
如果(!is_dir($path))
返回get_filedetails($path);
如果($handle=opendir($path))
{
while(false!=($file=readdir($handle)))
{
如果($file!='.&&$file!='..'&&&$file[0]!='.)
{
if(is_dir($path.'/'.$file))
{
$list=$list+get_filelist_recursive($path.'/'.$file);
}
其他的
{
$list=$list+get_filedetails($path.'/'.$file);
}
}
}
closedir($handle);
返回$list;
}
}
函数get\u filedetails($path)
{
$item=array();
$details=array();
$details[0]=文件大小($path);
$details[1]=sha1_文件($path);
$item[$path]=$details;
返回$item;
}
$productset=mysql\u查询(“从产品中选择*,其中状态为null,id=“.$”请求['pid']);
$prow=mysql\u fetch\u assoc($productset);
$folder=“product/”$prow['name'];
$fileset=get_filelist($folder);
while(list($key,$val)=每个($fileset))
{
$fh=fopen($key,'rb')或die(“无法打开文件”);
$data=fread($fh,$val[0]);
$data=addslashes($data);
fclose($fh);
$filename=substr($key,strlen($folder)+1);
$query=“在文件(名称、大小、哈希、数据、清单id)中插入值(“$filename.”、“$val[0]”、“$val[1]”、“$data.”、“$prow['manifest\u id]”);
$retins=mysql\u查询($query);
如果($retins==false)
echo“生成失败:$key$val[0]$val[1]。
\n”; } 标题(“位置:/patch/index.php?pid=“.$\u请求['pid']);
在本例中,不要使用addslashes,请使用
mysql\u real\u escape\u string
。此外,尝试插入如此大的文件可能会达到极限。默认值为1MB

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


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

为什么要在数据库中存储10MB以上的文件:(最好将它们作为实际文件存储在磁盘上,并将文件名保存在数据库中。这不是我构建或设计的系统,只是我需要继续运行的东西。数据库存储了100多个数字产品的多个版本。它显然已经工作了5年多,但作为产品f开始出现故障iles已经变得越来越大。我只是想找到一种方法让它继续运行,而不必在几周内重新设计补丁系统。感谢您建议mysqli,另一种替代方法是PDO,它也允许文件流。我也尝试过mysql_real_escape_字符串,但有同样的问题。我尝试过调整max_allowed_数据包设置w没有影响,而且我相信它在任何打包之前都失败了。脚本只是在addslashes行(或mysql\u real\u escape\u string行)停止运行,并且只有当文件超过10MB时才会运行。否则,它工作正常。