【问题标题】:Laravel file does not exist - file uploadLaravel 文件不存在 - 文件上传
【发布时间】:2014-04-18 19:05:30
【问题描述】:

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

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

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

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

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

【问题讨论】:

  • 如果您解决了您的问题,请answer your own question,这样它就不会再显示为“未答复”,并作为其他人未来的参考。

标签: php file upload laravel


【解决方案1】:

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

upload_max_filesize = 20M
post_max_size = 20M

这应该可以正常工作。

【讨论】:

  • 最好发表评论说这个答案来自 OP,而不是把这个注释放在帖子本身(因为我们试图避免帖子的噪音)。大多数人可能会建议发布社区 Wiki。
  • 这是最好的答案,因为我被卡住了,找不到真正的原因,这确实解决了我的问题
【解决方案2】:

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

【讨论】:

  • 这已在 2014 年解决 + 接受的答案说明了要在 php.ini 文件中进行的更改。
【解决方案3】:

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

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

【讨论】:

    【解决方案4】:

    在 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.
        }
    

    如果文件大小超过最大大小,看起来getSize 方法成功返回 false。以我的经验isValid 抛出一个模糊的错误。 upload_max_filesize 参数是用来保护服务器的,所以我想要一种可靠的方法来捕捉用户尝试上传大文件并且我的客户端验证设置不正确。

    【讨论】:

      猜你喜欢
      • 2020-06-05
      • 1970-01-01
      • 2021-01-27
      • 1970-01-01
      • 2014-01-15
      • 1970-01-01
      • 1970-01-01
      • 2016-07-11
      • 2017-04-13
      相关资源
      最近更新 更多