Symfony2的分配上载文件到内存(Symfony2 allocates uploaded fil

2019-07-29 22:06发布

虽然上传文件比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 %}

Answer 1:

我认为您是通过app_dev.php前端控制器试图symfony的。

默认情况下,在开发环境,剖析对每个请求启用。

framework:
    profiler: { only_exceptions: false }

只要改变从假only_exceptions参数去真实的,然后再试一次。

框架:探查:{only_exceptions:真正}

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

行动:

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不会超过内存使用情况。 如果文件过大,形式显示错误,且里面的代码“如果($形式 - > isValid()的)”不执行。

看一看形式的文档: http://symfony.com/doc/current/book/forms.html和“文件”字段类型参考: http://symfony.com/doc/current/reference/形式/类型/ file.html



文章来源: Symfony2 allocates uploaded file to memory