【问题标题】:symfony2 fileupload (Fatal Error: Allowed memory size exhausted)symfony2 文件上传(致命错误:允许的内存大小已用尽)
【发布时间】:2023-03-25 07:55:01
【问题描述】:

我遇到了一个非常奇怪的问题。

我尝试用 Symfony2 和 Doctrine 实现一个简单的附件上传器。

所以我关注了这篇食谱文章http://symfony.com/doc/current/cookbook/doctrine/file_uploads.html

一切似乎都很好,但是当我尝试上传一个 140KB 的文件时,PHP 抛出了一个致命错误。

这是我的验证:

/**
 * @Assert\File(
 *      maxSize="4M",
 *      maxSizeMessage="Allowed maximum size is {{ limit }}"
 * )
 */
public $file;

这是我的 php.ini

memory_limit = 256M
max_input_time = 60
max_execution_time = 30
file_uploads = On
upload_max_filesize = 128M

所以一切都应该没问题,脚本需要大约 500 毫秒才能得到错误: 致命错误:允许的内存大小为 268435456 字节已用尽(尝试分配 122 字节)

当我增加 memory_limit 时,它不会改变任何东西,内存大小会增加,但它仍然尝试分配 122 个字节。 其他一切正常,只是文件上传中断。

大家猜猜是怎么回事?

编辑: 是的,我做了{{ form_enctype(form) }},所以我会发布uploadAction。这很复杂,因为模型充满了外键,我会剪掉那些无关紧要的部分:

public function createAction(Request $request)
    {
        $em = $this->getDoctrine()->getEntityManager();

        $conference = new Conference();
        $conference->setArchived(false);
        $conference->setLastModification(new \DateTime());

        $form = $this->createForm(new ConferenceType(), $conference);

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

            if ($form->isValid()) {
                $registrationForm = $conference->getRegistrationForm();

                $em->persist($conference->getLocation());

                $conference->getImage()->upload();
                $em->persist($conference->getImage());

                // lots of other stuff is going on (does not refer to the upload processs

                $em->flush();

                return $this->redirect($this->generateUrl('KnowHowERegistrationBackendBundle_registrationform_create'));
            }
        }

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

我希望有人能够重现它或找到解决方案。

【问题讨论】:

  • 只是在黑暗中的狂野拍摄,但您的模板中有 form_enctype(form) 吗?如果你这样做了,那么可能会发布你的 uploadAction 代码。可能在某个地方也有递归。

标签: php symfony doctrine


【解决方案1】:

什么都没有真正跳出来。严格来说,这是一种解决问题的答案。

首先注释掉这些行:

            $conference->getImage()->upload();
            $em->persist($conference->getImage());

验证其他一切是否正常。并发布完整的错误消息。里面可能有线索。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-10-05
    • 1970-01-01
    • 2015-05-30
    • 2017-12-13
    • 2014-04-07
    • 2016-09-24
    • 2012-12-27
    相关资源
    最近更新 更多