在Symfony中上傳大於post_max_size的文件時,上傳的文件被分配到內存中。Symfony2將上傳的文件分配給內存
Fatal error: Allowed memory size of 150994944 bytes exhausted (tried to allocate 84627994 bytes) in /Applications/MAMP/htdocs/Symfony/vendor/symfony/src/Symfony/Component/HttpKernel/DataCollector/DataCollector.php on line 28
爲什麼symfony中試圖分配一個文件上POST內存?
1/php.ini中
file_uploads = On
upload_tmp_dir = /Applications/MAMP/tmp/php
upload_max_filesize = 32M
post_max_size = 48M
2 /控制器
<?php
//AcmeDemoBundle/Controller/DemoController
namespace Acme\DemoBundle\Controller;
use Symfony\Component\HttpFoundation\Request;
class DemoController extends Controller
{
public function createAction(Request $request)
{
if ($request->getMethod() == 'POST')
{
if ($_FILES["file"]["size"] < 3000000)//3Mb
{
if ($_FILES["file"]["error"] > 0)
{
echo "Return Code: " . $_FILES["file"]["error"] . "<br />";
}
else
{
if (empty($_POST) && empty($_FILES) && isset($_SERVER['REQUEST_METHOD']) && strtolower($_SERVER['REQUEST_METHOD']) == 'post')
{
echo "The file is bigger than post_max_size in php.ini.";
}
else
{
echo "Size: " . ($_FILES["file"]["size"]/1024) . " Kb<br />";
if (file_exists("upload/" . $_FILES["file"]["name"]))
{
echo $_FILES["file"]["name"] . " already exists. ";
}
else
{
move_uploaded_file($_FILES["file"]["tmp_name"],
"upload/" . $_FILES["file"]["name"]);
echo "Stored in: " . "upload/" . $_FILES["file"]["name"];
}
}
}
}
else
{
echo "Invalid file";
}
}
return $this->container->get('templating')->renderResponse('AcmeDemoBundle:Demo:create.html.twig');
}
3 /模板
//AcmeDemoBundle/Resources/views/Demo/create.html.twig
{% extends "AcmeDemoBundle::layout.html.twig" %}
{% block content %}
<h1>Upload File</h1>
<form action="#" method="post"
enctype="multipart/form-data">
<label name="file">Filename:</label>
<input type="file" name="file" id="file" />
<br />
<input type="submit" name="submit" value="Submit" />
</form>
{% endblock %}
嗨阿德里安,我真的很感謝你的幫助,因爲我還是堅持了這個問題......你」對了,我使用了app_dev.php前端控制器。我試圖將only_exceptions設置爲false,但仍然收到此錯誤消息。我在prod中遇到了同樣的問題,這就是爲什麼我非常擔心:-)當我發佈表單時,我可以看到文件仍在加載到內存中。我不知道在哪裏和什麼組件加載這個文件到內存,雖然...它可能是ORM?有沒有辦法檢查?非常感謝:-) – Mick
嗨Adrien,不,這是我第一次嘗試,這就是爲什麼我去普通的PHP代碼....請嘗試你的代碼,如果你有一分鐘或2,但我相信你會有相同的錯誤信息。非常感謝您的幫助。 – Mick
嗨,我實際上試過的代碼,它的工作,而你的版本引發了內存錯誤。 – AdrienBrault