概述
标签:php
我试图用fsockopen上传一个2gb以上的大文件.但是对于file_get_content,出现以下错误,我无法将大文件存储在内存中.我需要以块的形式发送数据,但无法弄清楚我将如何执行此操作.请有人能指导我吗?
Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 727826869 bytes) in
$file_info = finfo_open(FILEINFO_MIME);
$mime_type = finfo_file($file_info, $file_loc);
$file = file_get_contents($file_loc);
$fp = fsockopen($host, 80, $errno, $errstr, 30);
if ($fp) {
$out = "POST /upload/".basename($url)." HTTP/1.1rn";
$out .= "Host: ".$host."rn";
$out .= "Accept-Language: en-US,en;q=0.8rn";
$out .= "Content-Type: ".$mime_type."rn";
$out .= 'Content-Length: ' . filesize($file_loc) . "rn";
$out .= "Content-Disposition: attachment; filename="".urlencode(basename($file_loc)).""rnrn";
fwrite($fp, $out);
fwrite($fp, $file);
$response = '';
while (!feof($fp)) {
$response .= fgets($fp, 128);
}
fclose($fp);
解决方法:
file_get_contents将整个文件的内容作为变量返回.在你的情况下,这意味着它将尝试创建2GB变量,耗尽允许的内存.
尝试使用fopen和fgets.这将允许您以较小的块处理文件.
$file_info = finfo_open(FILEINFO_MIME);
$mime_type = finfo_file($file_info, $file_loc);
$fileHandle = fopen($file_loc);
$fp = fsockopen($host, 80, $errno, $errstr, 30);
if ($fp) {
$out = "POST /upload/".basename($url)." HTTP/1.1rn";
$out .= "Host: ".$host."rn";
$out .= "Accept-Language: en-US,en;q=0.8rn";
$out .= "Content-Type: ".$mime_type."rn";
$out .= 'Content-Length: ' . filesize($file_loc) . "rn";
$out .= "Content-Disposition: attachment; filename="".urlencode(basename($file_loc)).""rnrn";
fwrite($fp, $out);
while(!feof($fileHandle)){
fwrite($fp, fgets($fileHandle, 1024));
}
fclose($fileHandle);
$response = '';
while (!feof($fp)) {
$response .= fgets($fp, 128);
}
fclose($fp);
标签:php
来源: https://codeday.me/bug/20191007/1867939.html
最后
以上就是优秀绿草为你收集整理的php get上传文件,php – file_get_contents大文件上传的全部内容,希望文章能够帮你解决php get上传文件,php – file_get_contents大文件上传所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复