简体   繁体   English

PHP fwrite()块到一个大文件/ 504网关超时

[英]PHP fwrite() chunks to one large file / 504 Gateway timeout

I'm working on an upload tool to upload large files (1GB - 4GB) to a FTP server. 我正在使用一种上传工具,可以将大文件(1GB-4GB)上传到FTP服务器。 I used HTML5 to slice the uploaded file into 1MB chunks and then upload each of those chunks to a temp folder. 我使用HTML5将上传的文件切成1MB的块,然后将每个块上传到temp文件夹。 As soon as every chunk is uploaded a PHP script creates the file out of those chunks: 只要上传了每个块,PHP脚本就会从这些块中创建文件:

if (($fp = fopen(UPLOAD_DIR.$fileName, 'w')) !== false) {
    for ($i = 1; $i <= $totalFiles; $i++) {
        fwrite($fp, file_get_contents($tempDir.'/'.$fileName.'.part'.$i));               
    }
    fclose($fp);        
}

Everything works for smaller file sizes (tested with files around 1GB - 1.5GB), but as soon as i upload very large files (eg 3GB) my PHP script stops with a 504 Gateway timeout. 一切适用于较小的文件大小(经过1GB-1.5GB的文件测试),但是一旦我上传非常大的文件(例如3GB),我的PHP脚本就会因504网关超时而停止。 The 'max_execution_time' in my php.ini is set to 90. Is there any way to avoid the 504 timeout in this case or a way to speed up the script, which creates the file? 我的php.ini中的'max_execution_time'设置为90。在这种情况下,是否有任何方法可以避免504超时,或者可以加速创建文件的脚本? I could try to change 'max_execution_time', but that doesn't seem to be the perfect solution. 我可以尝试更改“ max_execution_time”,但这似乎并不是完美的解决方案。 Any suggestions? 有什么建议么?

EDIT: The script is running on a Linux System 编辑:脚本正在Linux系统上运行

I don't know what operating system your server is running, but if it is linux, you could try it directly from the command line using for example exec . 我不知道您的服务器正在运行什么操作系统,但是如果它是linux,则可以使用exec直接在命令行中尝试。

Something like: 就像是:

exec('cat ' . $tempDir . '/' . $fileName . '.part* > ' . UPLOAD_DIR . $fileName);

You might need to use escapeshellcmd() on your variables if you don't control them. 如果您不控制变量,则可能需要在变量上使用escapeshellcmd()

You can also generate a list of partial file names in a loop if .part* does not give you the correct order. 如果.part*没有给您正确的顺序,您还可以在循环中生成部分文件名的列表。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM