在 Symfony 中上传大于post_max_size的文件时,上传的文件被分配在内存中。
致命错误:第 28 行 /Applications/MAMP/htdocs/Symfony/vendor/symfony/src/Symfony/Component/HttpKernel/DataCollector/DataCollector.php 中允许的内存大小为 150994944 字节(试图分配 84627994 字节)
为什么 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 %}