Symfony2将上传的文件分配给内存

时间:2012-06-14 16:11:47

标签: php symfony symfony-forms

在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 / THE CONTROLLER

<?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 / THE TEMPLATE

//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 %}

1 个答案:

答案 0 :(得分:11)

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

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

framework:
    profiler: { only_exceptions: false }

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

框架:         profiler:{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永远不会超过内存使用量。如果文件太大,表单会显示错误,并且代码内有&#34; if($ form-&gt; isValid())&#34;没有执行。

查看表单文档:http://symfony.com/doc/current/book/forms.html 以及&#34;文件&#34;的参考;字段类型:http://symfony.com/doc/current/reference/forms/types/file.html