4

在 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 %}
4

1 回答 1

11

我假设您正在通过 app_dev.php 前端控制器尝试 symfony。

默认情况下,在开发环境中,为每个请求启用分析器。

framework:
    profiler: { only_exceptions: false }

只需将 only_exceptions 参数从 false 更改为 true,然后重试。

框架:分析器:{ only_exceptions: true }

您应该使用框架为您提供的工具来处理文件上传。

行动:

public function createAction(Request $request)
{
    $form = $this->createFormBuilder()
        ->add('attachment', 'file')
        ->getForm();

    if ($request->getMethod() == 'POST') {
        $form->bindRequest($request);

        if ($form->isValid()) {
            $file = $form['attachment']->getData(); /** @var $file \Symfony\Component\HttpFoundation\File\UploadedFile */

            echo $file->getClientSize();
        }
    }

    return $this->render('AcmeDemoBundle:Demo:create.html.twig', array(
        'form' => $form->createView(),
    ));
}

模板:

{% extends "AcmeDemoBundle::layout.html.twig" %}

{% block content %}

<h1>Upload File</h1>

<form action="" method="post" {{ form_enctype(form) }}>
    {{ form_errors(form) }}

    {{ form_row(form.attachment) }}

    {{ form_rest(form) }}

    <input type="submit" />
</form>

{% endblock %}

使用此代码,php 永远不会超过内存使用量。如果文件太大,表单会报错,“if ($form->isValid())”里面的代码不会被执行。

查看表单文档:http ://symfony.com/doc/current/book/forms.html 和“文件”字段类型的参考: http: //symfony.com/doc/current/reference/表格/类型/file.html

于 2012-06-15T19:16:28.817 回答