[英]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.