简体   繁体   中英

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

在此处输入图像描述

I am getting this error while trying to upload a file, but the file is being uploaded cause I can find it in the directory, but instead of proceeding, it throws this error, and I have been using this script for long until laravel 6.0 and I don't get the reasons why! thanks

$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');
}

here is my Html code

<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

                  ```

Сheck that the call $validator->fails() occurs once.

In my case, the first call $validator->fails() in my Controller action, after checking deleted the file. The second call could not find this file.

If the file is successfully uploaded, so the problem is somewhere else, but unfortunately this error representing is so confusing and is not the actual error.
The actual error is happening where you're inserting data into database, in your case is this line:

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

and in my case was a line I was trying to insert a blog to database:

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

In order to find out what is the real error you have to use try and catch.

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

when you dump and die $e , you will see the actual error:

在此处输入图像描述

for me, In my case the actual error was: " Field 'author_type' doesn't have a default value ", but for some unknown reasons laravel 6.0 shows " file does not exists or is not readable error " when you have uploaded data within your form.

If someone else hits this error, try checking your php.ini...make sure that your upload_max_filesize is as big as your post_max_size. I had 1000M for post_max_size, but only 100M for upload_max_size. The upload would churn for a long time, and then throw the error above (except it had "" as the file name, and no file was uploaded).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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