【发布时间】:2015-06-10 13:17:11
【问题描述】:
我一直在颠倒互联网,以便让分块的大文件上传为我的 Symfony2 应用程序工作。为了简化事情,我将测试程序与我的应用程序的其余部分分开。它唯一做的就是尝试用本文描述的方法测试 php://input 是否可以接受我的大文件上传:
http://www.webiny.com/blog/2012/05/07/webiny-file-upload-with-html5-and-ajax-using-php-streams/
我的理解是,如果用户上传 2GB+ 的大型视频文件,那么服务器上将占用 2GB+ 的 RAM,并且文章中的方法将其减少到每个并发上传者的 4096 位。 (我不确定如何在 localhost 服务器上测试 RAM 使用情况)
但是,当我尝试上传大文件时,浏览器只需等待一两秒,然后发布一个 1kb 的视频文件。
我为 php.ini 设置了较高的值。
这是PHP代码的相关部分:
public function lastoppAction(Request $request)
{
$videofilename = (string)$request->request->get("filnavn");
$inputHandler = fopen("php://input", "rb");
$fileHandler = fopen("videoer\\myfile.tmp", "wb") or die('fopen failed');;
while(true) {
$buffer = fgets($inputHandler, 4096);
if (strlen($buffer) == 0) {
fclose($inputHandler);
fclose($fileHandler);
break;
}
fwrite($fileHandler, $buffer) or die('fwrite failed');;
}
$url = $this->generateUrl('largefile_navngi', array('filnavn' => $videofilename));
return $this->redirect($url);
}
public function navngiAction(Request $request)
{
$filnavn = $request->query->get('filnavn');
$sti = $this->container->getParameter('kernel.root_dir');
$websti = preg_replace('/app/','web',$sti,1);
print_r("Filnavn: " . $filnavn);
rename($websti . "/videoer/myfile.tmp",$websti . "/videoer/" . $filnavn);
$url = $this->generateUrl('largefile_index');
return $this->redirect($url);
}
重定向的原因是为了重命名正确接收文件名变量。
AJAX 代码类似于上面链接的文章,但有以下区别:
xhr.open("POST", url, false); 使用我自己的 localhost 网址。我也在这里尝试过 true 。不同之处在于,如果我们在第三个参数中设置为 false,它适用于小文件。
使用 async true 似乎只给两个文件我打开到的原始 myfile.tmp 和一个它重命名的 0kb 文件。 “文件名.mp4”。
奇怪的是,当ajax代码运行时:
xhr.onreadystatechange = function()
{
if(xhr.readyState == 4)
{
if(xhr.status == 200)
{
//alert("success");// process success
alert("Filnavn: " + filnavn);
}else{
// process error
alert("Status: " + xhr.status);
}
}
};
即使 16 MB 这样的小文件在没有其他问题的情况下完全上传,状态也会变为 500。
这是树枝文件:
<h1>Test av å laste opp større filer</h1>
<form method="post" action="{{path('largefile_lastopp')}}">
<label>Fil: </label>
<input type="file" name="fil" id="video" /><br /><br />
<input type="hidden" name="filnavn" id="filnavn" />
<input type="hidden" name="mimetype" id="mimetype" />
<input onclick="upload('video',0)" type="submit" name="submit" />
</form>
::1 - - [10/Jun/2015:15:41:36 +0200] "POST /LargeFileUpload/web/app_dev.php/lastopp HTTP/1.1" 302 436 "http://localhost/LargeFileUpload/web/app_dev.php/" "Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/43.0.2357.81 Safari/537.36"
【问题讨论】:
-
可能服务器配置为低于所需大小的
MAX POST SIZE。 -
我认为您在文章中将标准文件上传与另一种方式混合在一起。看文章html。完全不同
-
我有一个很大的 MAX POST SIZE
-
你已经配置了
upload_max_filesize? -
@Paulius S.upload_max_filesize=2000M
标签: php ajax symfony file-upload