简体   繁体   中英

Can't write image data to path. Laravel

Okay I can't find help with this anywhere.

ALL of my paths are set to 755 (which should be sufficient access) but I still get the error.

My storage and public folders are linked.

Here's my code:

if (Input::hasFile('file')) {
  $image = Image::make($_FILES['file']['tmp_name']);
  $image->fit(200);
  $image->save('public/avatars/user77.jpg');
}

Are you using Intervention for Image ? I ran into a similar issue a few months ago. I ended up getting the image data from Image using stream() and then using Laravel's Storage facade to save it like this.

use Illuminate\Support\Facades\Storage;

if (Input::hasFile('file')) {
  $image = Image::make($_FILES['file']['tmp_name'])->fit(200)->stream('jpg');
  Storage::put('public/avatars/user77.jpg', $image);
}

See this article for more about Laravel's filesystem if needed.

check for directory existence , if there is no such directory you will create it :

 if (!file_exists($save_path)) {
            mkdir($save_path, 666, true);
        }

I was able to come up with a solution without the need for Intervention , which my project does not allow:

if ($request->hasFile('file')) {
  $image = file_get_contents($_FILES['file']['tmp_name']);
  Storage::disk('images')->put('user77.jpg', $image);
}

file_get_contents from my understanding is usually for external urls and what have you, but this solution worked for my images. This was especially handy because I'm using the Froala editor, so I am limited in how I send the request.

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