Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/284.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php Symfony2-上传前验证文件大小_Php_Symfony_File Upload - Fatal编程技术网

Php Symfony2-上传前验证文件大小

Php Symfony2-上传前验证文件大小,php,symfony,file-upload,Php,Symfony,File Upload,当用户上载文件时,我正在验证文件大小,但当文件太大时,我会得到奇怪的结果: 控制器: // Attachments form $form = $this->createFormBuilder() ->add('file', 'file', array('label' => ' ', )) ->getForm(); if ($this->getRequest()->isMethod('post') && $form-&

当用户上载文件时,我正在验证文件大小,但当文件太大时,我会得到奇怪的结果:

控制器:

// Attachments form

$form = $this->createFormBuilder()
    ->add('file', 'file', array('label' => ' ',
    ))
    ->getForm();

if ($this->getRequest()->isMethod('post') && $form->bind($this->getRequest())->isValid()) {
    $uploadedFile = $form['file']->getData();
    ...
}
实体:

namespace Pro\Bundle\Entity;

use Symfony\Component\HttpFoundation\File\File as SymfonyFile;

/**
 * @ORM\Entity
 * @ORM\Table(name="file")
 */
class File
{
    /**
     * @ORM\Id
     * @ORM\Column(type="integer")
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;
    
    //more properties...

    /**
     * @var SymfonyFile
     * @Constraints\File(maxSize=2000000) // <2 MB
     */
    private $filesystemFile;

    function __construct(SymfonyFile $file, $name)
    {
        $this->created = new \DateTime;
        $this->setFilesystemFile($file);
        $this->name = $name;
    }

    //more methods...

    function setFilesystemFile(SymfonyFile $file)
    {
        $this->filesystemFile = $file;
    }
}
namespace-Pro\Bundle\Entity;
使用Symfony\Component\HttpFoundation\File\File作为SymfonyFile;
/**
*@ORM\Entity
*@ORM\Table(name=“file”)
*/
类文件
{
/**
*@ORM\Id
*@ORM\Column(type=“integer”)
*@ORM\GeneratedValue(strategy=“AUTO”)
*/
私人$id;
//更多属性。。。
/**
*@var SymfonyFile
*@Constraints\File(maxSize=2000000)//created=new\DateTime;
$this->setFilesystemFile($file);
$this->name=$name;
}
//更多方法。。。
函数setFilesystemFile(SymfonyFile$文件)
{
$this->filesystemFile=$file;
}
}
因此,当用户提交文件时,
isValid
方法检查文件大小。如果大于200000B,将抛出一个错误。但我得到了奇怪的结果:

在本地主机中(
upload\u max\u filesize=2M
post\u max\u size=2M
):

如果文件太大,则会出现两个错误:

令牌无效

上载的文件太大

在VPS中(
upload\u max\u filesize=2M
post\u max\u size=2M
):


如果文件太大,请先尝试上载该文件,然后引发内部服务器错误。查看日志,我可以看到错误是由于无效实体造成的,因为文件太大。因此,它似乎试图移动文件,即使文件太大…

您是否尝试在实体文件中使用类似的函数,并使用validation.yml文件添加验证

public function isFilesystemFile()
{

  $valid = false;

  //do the validation such as file size is too large. 
  //then set the valid flag accordingly

  return $valid;

}
在validation.yml文件中

Pro\Bundle\Entity\FilesystemFile:
getters:
    FilesystemFile:
        - "True": { message: "File size is too large." }
希望这有帮助


干杯

你核对了我下面的答案吗?这不会改变任何事情