apache 大型 .PDF 文件未通过 PHP 作为中等 BLOB 上传到 MySQL 数据库,2MB 以下的文件工作正常

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1573581/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 18:17:17  来源:igfitidea点击:

Large .PDF Files Not Uploading To MySQL Database as Medium BLOB Via PHP, Files under 2MB Work Fine

phpmysqlapacheblob

提问by dbaugh

I am developing a PHP script for uploading .PDF documents as medium BLOBs into a MySQL database via PHP. The script also allows users to search for files and open/download them but I do not think that part of the script is relevant to my issue. The script works fine with files less than 2 MB but as soon as I try and upload a file that is more than 2 MB nothing is going into my content(Medium BLOB) column and there is no value for the mime type. I have already tried increasing the max_packet_size for the MySQL server to 4 MB from its default value of 1 MB. I have also updated php.ini to the correct values, I think. I set upload_max_size to 4MB, post_max_size to 4 MB, and memory_limit to 16 MB. I haven't really experimented with the max_input_time though because it is defaulted to 60 seconds which seems like plenty considering this script is for an internal application on an intranet.

我正在开发一个 PHP 脚本,用于通过 PHP 将 .PDF 文档作为中等 BLOB 上传到 MySQL 数据库中。该脚本还允许用户搜索文件并打开/下载它们,但我认为该部分脚本与我的问题无关。该脚本适用于小于 2 MB 的文件,但是一旦我尝试上传超过 2 MB 的文件,我的内容(中型 BLOB)列中就没有任何内容,并且 mime 类型没有任何值。我已经尝试将 MySQL 服务器的 max_packet_size 从其默认值 1 MB 增加到 4 MB。我也将 php.ini 更新为正确的值,我想。我将upload_max_size 设置为4MB,post_max_size 设置为4 MB,memory_limit 设置为16 MB。我没有

I have also tried to catch errors from the PDO object in my script with errorInfo() and errorCode() and both through 0s (no error).

我还尝试使用 errorInfo() 和 errorCode() 以及两者都通过 0(无错误)从脚本中的 PDO 对象中捕获错误。

Here is the upload section of my script:

这是我的脚本的上传部分:

if ($key == 'upload')
{
    set_time_limit(0);
    $_SESSION['upload'] = $value;

    if ($_FILES['userfile']['name'])
    {
        $fileName = $_FILES['userfile']['name'];
        $tmpName  = $_FILES['userfile']['tmp_name'];
        $fileSize = $_FILES['userfile']['size'];
        $fileType = $_FILES['userfile']['type'];
        $docType = $_POST['docType'];
        $netKey = $_POST['netKey'];
        $fp       = fopen($tmpName, 'r');
        $content  = fread($fp, filesize($tmpName));
        $content  = addslashes($content);
        fclose($fp);

        $_SESSION['filetype'] = $fileType;

        if(!get_magic_quotes_gpc())
        {
            $fileName = addslashes($fileName);
        }
        $sqlCheck = "SELECT id, name, documentType, networkKey FROM upload WHERE active='1'";
        foreach ($dbh->query($sqlCheck) as $row)
        {
            if (($row['name'] == $fileName) && ($row[networkKey] == $netKey) && ($row['documentType'] == $docType)) 
            {
                $_SESSION['updateRow'] = $row['id'];
            }

        }

        if ($_SESSION['updateRow'])
            {
                $deactivateDuplicate = "UPDATE upload SET active = 0, modifiedBy = '".strtoupper($_SERVER['REMOTE_USER'])."', modifiedDate = Now() WHERE id = '".$_SESSION['updateRow']."' AND active ='1'";
                $dbh->query($deactivateDuplicate);
                $_SESSION['sql'] = "SELECT id, name, documentType, type, size, networkKey, modifiedBy, modifiedDate, active FROM upload WHERE active = '1'";
                $_SESSION['uploadSearch'] = 1;
                $_SESSION['updateRow'] = 0;
            }
            $values = "'".$fileName."','".$docType."','".$fileType."','".$content."','".$fileSize."','".$netKey."','".strtoupper($_SERVER['REMOTE_USER'])."', Now(), 1";
            $sqlUpload = "INSERT INTO upload (name, documentType, type, content, size, networkKey, modifiedBy, modifiedDate, active) VALUES (".$values.")";
            $dbh->query($sqlUpload);

            $_SESSION['sql'] = "SELECT id, name, documentType, type, size, networkKey, modifiedBy, modifiedDate, active FROM upload WHERE active = '1'";
            $_SESSION['uploadSearch'] = 1;


    }
}

And Here is the front end of the application showing that a smaller file works and a larger file does not. The file sample.pdf is less than 1 MB and the file test.pdf is 2.5 MB.

这是应用程序的前端,显示较小的文件有效,而较大的文件无效。文件 sample.pdf 小于 1 MB,文件 test.pdf 为 2.5 MB。

http://www.imagechicken.com/viewpic.php?p=1255628359017775300&x=png

http://www.imagechicken.com/viewpic.php?p=1255628359017775300&x=png

I would have used img tags but alas I am a new user and not allowed.

我会使用 img 标签,但可惜我是新用户,不允许使用。

The next thing I am going to look for is a setting in Apache that limits file sizes. I am new to LAMP so any tips would be great. Thanks

接下来我要寻找的是 Apache 中限制文件大小的设置。我是 LAMP 的新手,所以任何提示都会很棒。谢谢

采纳答案by dbaugh

I actually fixed the issue. I changed all of the values that were recommended here in php.ini and my.cnf but I also needed to change a setting for PDO.

我实际上解决了这个问题。我更改了 php.ini 和 my.cnf 中推荐的所有值,但我还需要更改 PDO 的设置。

I changed: PDO::MYSQL_ATTR_MAX_BUFFER_SIZE (integer) Maximum buffer size. Defaults to 1 MiB.

我更改了: PDO::MYSQL_ATTR_MAX_BUFFER_SIZE (integer) 最大缓冲区大小。默认为 1 MiB。

This has to be set when the PDO object is created to work though. All is good now.

这必须在创建 PDO 对象时设置才能工作。现在一切都很好。

回答by andrewWinn

YOu might be running up against the limitations of the HTTP request (which I think is 4MB), or your HTTP request might be timming out. I have run into this issue before. You might want to look @ your php.ini file for maximum upload size.

您可能会遇到 HTTP 请求(我认为是 4MB)的限制,或者您的 HTTP 请求可能会超时。我以前遇到过这个问题。您可能想查看@您的 php.ini 文件以获得最大上传大小。

Disclaimer: I am not a PHP developer :D

免责声明:我不是 PHP 开发人员 :D

回答by Jason

You might be running into a mySQL limit.

您可能会遇到 mySQL 限制。

Run the following in mySQL:

在 mySQL 中运行以下命令:

show variables like 'max_allowed_packet'  

That will show you your current setting. The default is 1047552 bytes.

这将显示您当前的设置。默认值为 1047552 字节。

This can be changed with your my.cnf file. Simply add

这可以通过您的 my.cnf 文件进行更改。只需添加

set-variable = max_allowed_packet=2M

or what ever size is required and restart mySQL.

或者需要什么大小并重新启动mySQL。

EDIT:
What about

编辑
怎么样

<input type="hidden" name="MAX_FILE_SIZE" value="xxx" />

Might that be part of the problem?

这可能是问题的一部分吗?

回答by Andrew Coleson

So to recap all the things I believe I had to set in order to allow file uploads >1MB on something I was working on:

所以回顾一下我认为我必须设置的所有内容,以便在我正在处理的事情上允许文件上传> 1MB:

php.ini

配置文件

  • Changed post_max_size(I wanted 15MB file uploads, so I set it to 20Mjust in case)
  • Changed upload_max_filesizeto 15M
  • Left max_execution_timeat 60
  • Left max_input_timeat 60
  • Left default_socket_timeoutat 60
  • 已更改post_max_size(我想要 15MB 文件上传,所以我将其设置为20M以防万一)
  • 改变upload_max_filesize15M
  • max_execution_time60
  • max_input_time60
  • default_socket_timeout60

my.ini

我的.ini

  • Changed max_allowed_packetto 15M
  • 改变max_allowed_packet15M

PHP Code

PHP代码

  • changed my PDO call to:

    new PDO("mysql:host=$host;dbname=$db_name", $db_username, $db_password, array (PDO::MYSQL_ATTR_MAX_BUFFER_SIZE=>15*1024*1024))

  • 将我的 PDO 调用更改为:

    new PDO("mysql:host=$host;dbname=$db_name", $db_username, $db_password, array (PDO::MYSQL_ATTR_MAX_BUFFER_SIZE=>15*1024*1024))

回答by Lee Benson

If you're trying to do this on shared hosting (where you can't make changes to MySQL conf files, etc), Google 'mysqli prepared statements'.

如果您尝试在共享主机上执行此操作(您无法更改 MySQL conf 文件等),请使用 Google 'mysqli 准备语句'。

This allows you to upload large queries in predefined byte chunks, circumventing any buffer restrictions.

这允许您以预定义的字节块上传大型查询,从而绕过任何缓冲区限制。