我假设您正在通过 app_dev.php 前端控制器尝试 symfony。
默认情况下,在开发环境中,分析器会针对每个请求启用。
framework:
profiler: { only_exceptions: false }
只需将 only_exceptions 参数从 false 更改为 true,然后重试。
框架:
探查器:{ only_exceptions:true }
您应该使用框架提供的工具来处理文件上传。
Action:
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/book/forms.html以及“文件”字段类型的参考:http://symfony.com/doc/current/reference/forms/types/file.html http://symfony.com/doc/current/reference/forms/types/file.html