Laravel 文件不存在 - 文件上传

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22388926/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 09:11:07  来源:igfitidea点击:

Laravel file does not exist - file upload

phpfileuploadlaravel

提问by cch

I am using a form to upload video files. For some reason all I get is the following error:

我正在使用表单上传视频文件。出于某种原因,我得到的只是以下错误:

Symfony \ Component \ HttpFoundation \ File \ Exception \ FileNotFoundException
The file "" does not exist

In my controller I had a validation rule to require files, like so

在我的控制器中,我有一个需要文件的验证规则,就像这样

$validator = Validator::make(Input::all(),
  array(
    'file'  => 'required'
  )
);

But because of the above rule, I couldn't properly debug what is going on, therefore I ended up removing it, as a result to generate the above error.

但是由于上述规则,我无法正确调试正在发生的事情,因此我最终将其删除,结果产生了上述错误。

回答by menjaraz

In the php.ini file, change the following:

在 php.ini 文件中,更改以下内容:

upload_max_filesize = 20M
post_max_size = 20M

This should make it work.

这应该使它工作。

回答by jose.serapicos

Probably the problem is the "upload_max_filesize" that has been exceeded (check your php.ini), however it is a good practice to first check if the file is valid.

问题可能是超出了“upload_max_filesize”(检查您的 php.ini),但是首先检查文件是否有效是一个好习惯。

if (!$file->isValid()) {
    throw new \Exception('Error on upload file: '.$file->getErrorMessage());
}
//...

回答by DavidHyogo

In 2020 with Laravel 5.8, I discovered this problem and your answers gave me clues, but I was still getting an error when I tried the isValidmethod. I found this was the best way to check if a file was too large:

在 2020 年的 Laravel 5.8 中,我发现了这个问题,您的回答为我提供了线索,但是当我尝试该isValid方法时仍然出现错误。我发现这是检查文件是否太大的最佳方法:

    $file_result = new \stdClass();
    if ($request->file('file_to_upload')->getSize() === false) {
        $max_upload = min(ini_get('post_max_size'), ini_get('upload_max_filesize'));
       //From: https://gist.github.com/svizion/2343619
        $max_upload = str_replace('M', '', $max_upload);
        $max_upload = $max_upload * 1024;
        $file_result->error = "The file you are trying to upload is too large. The maximum size is " . $max_upload;
        //return whatever json_encoded data your client-side app expects.
    }

It looks as if the getSizemethod successfully returns false if the file size exceeds the maximum size. In my experience isValidthrows an obscure error. The upload_max_filesize parameter is there to protect the server so I wanted a reliable way of catching when the user attempts to upload a large file and my client-side validation is not set correctly.

getSize如果文件大小超过最大大小,则该方法看起来好像成功返回 false。根据我的经验,isValid会抛出一个模糊的错误。upload_max_filesize 参数用于保护服务器,因此当用户尝试上传大文件并且我的客户端验证设置不正确时,我想要一种可靠的捕获方式。

回答by Ahmad Rezk

To clear the answer The problem that you uploaded file which size is more than php configuration located in php.ini, so when you try to access the any property of the uploadFile object you will see the exception because file not exist

清除答案您上传的文件大小超过位于 php.ini 中的 php 配置的问题,因此当您尝试访问 uploadFile 对象的 any 属性时,您将看到异常,因为文件不存在