虽然上传文件比Symfony的post_max_size要大,上传的文件在内存中分配。
致命错误:用尽150994944个字节允许存储器大小(试图分配84627994个字节)/Applications/MAMP/htdocs/Symfony/vendor/symfony/src/Symfony/Component/HttpKernel/DataCollector/DataCollector.php线路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 %}