繁体   English   中英

当图像已上传到文件夹中时,如何解决 laravel 中的“文件不存在或不可读”

[英]how to solve “file does not exist or is not readable” in laravel while the image have been uploaded in folder

在此处输入图像描述

我在尝试上传文件时收到此错误,但正在上传文件,因为我可以在目录中找到它,但它没有继续,而是抛出此错误,我一直在使用此脚本,直到laravel 6.0和我不明白为什么! 谢谢

$image_name=Str::random(12);
$ext=strtolower($image->getClientOriginalExtension());
$image_full_name=$image_name.'.'.$ext;
$upload_path='upload/article/';
$image_url=$upload_path.$image_full_name;
// $success=1;
$success=$image->move($upload_path,$image_full_name);
// return $success;
if($success)
{
    $data['post_photo']=$image_url;
    DB::table('post')->insert($data);
    Session::put('message','article posted Successfully');
    return Redirect::to('create-post');
}

这是我的 Html 代码

<div class="custom-file">
   <input type="file" class="custom-file-input form-control  " id="customFileLang" name="photo" >
   <label class="custom-file-label" for="customFileLang">Choose Picture </label>
</div>
@error("photo")
  <p style="color:red;" class="mx-4">{{ $message }}</p>
@enderror

                  ```

检查调用$validator->fails()发生一次。

就我而言,在检查删除文件后,在我的 Controller 操作中第一次调用$validator->fails() 第二次调用找不到这个文件。

如果文件上传成功,那么问题就出在其他地方,但不幸的是,这个错误表示的太混乱了,并不是真正的错误。
实际错误发生在您将数据插入数据库的地方,在您的情况下是这一行:

DB::table('post')->insert($data); // the case in this question

在我的情况下,我试图将博客插入数据库:

Blog::create($data); // my case

为了找出真正的错误,您必须使用 try 和 catch。

try {
  DB::table('post')->insert($data); // for my case : Blog::create($data);
} catch (\Exception $e) {
  dd($e);
}

当您转储并死掉$e时,您将看到实际错误:

在此处输入图像描述

对我来说,在我的情况下,实际错误是:“字段'author_type'没有默认值”,但由于某些未知原因,laravel 6.0 显示“文件不存在或不可读错误”当您在其中上传数据时你的表格。

如果其他人遇到此错误,请尝试检查您的 php.ini...确保您的 upload_max_filesize 与您的 post_max_size 一样大。 我有 1000M 用于 post_max_size,但只有 100M 用于 upload_max_size。 上传会搅动很长时间,然后抛出上面的错误(除了它有“”作为文件名,并且没有上传文件)。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM