vendredi 22 février 2019

Intervention Image can't write to storage_path

I want to store uploaded image to storage_path (storage/app/images/users) and not public_path (public/images/users) in my Laravel application.

My filesystem configuration is as follows:

'disks' => [

    'local' => [
        'driver' => 'local',
        'root' => storage_path('app'),
    ],

    'public' => [
        'driver' => 'local',
        'root' => storage_path('app/public'),
        'url' => env('APP_URL').'/storage',
        'visibility' => 'public',
    ],

    'users'=> [
        'driver' => 'local',
        'root' => storage_path('app/images/users'),
        'url' => env('APP_URL').'/storage'
    ],

    's3' => [
        'driver' => 's3',
        'key' => env('AWS_ACCESS_KEY_ID'),
        'secret' => env('AWS_SECRET_ACCESS_KEY'),
        'region' => env('AWS_DEFAULT_REGION'),
        'bucket' => env('AWS_BUCKET'),
        'url' => env('AWS_URL'),
    ],

],

I have also created the necessary folders in the desired path.

My upload script is as follows:

 public function upload_avatar($data) 
{

    $users_image_location=storage_path('images/users');
    $users_image_thumb_location_1=storage_path('images/users/thumbs-100');
    $users_image_thumb_location_2=storage_path('images/users/thumbs-50');
    $users_image_thumb_location_3=storage_path('images/users/thumbs-30');

    $image = $data['photo'];

    $filename = User::create_random_name(32) . '.' . $image->extension();

    $image_file = Image::make($image->getRealPath());

    list($width, $height) = getimagesize($image);

    if ($width > 700) 
    {
        //Resize the image
        $image_file->resize(600, 600, function ($constraint) {
            $constraint->aspectRatio();
        });            
    } 
    //save the image file
    $image_file->save($users_image_location.'/' . $filename);

    //create and store a 100px thumbnail of the image
    $image_file->resize(100, 100, function($constraint) {
        $constraint->aspectRatio();
    })->save($users_image_thumb_location_1.'/'.$filename);

    $image_file->resize(50, 50, function($constraint) {
        $constraint->aspectRatio();
    })->save($users_image_thumb_location_2.'/'.$filename);

    $image_file->resize(30, 30, function($constraint) {
        $constraint->aspectRatio();
    })->save($users_image_thumb_location_3.'/'.$filename);

    //return the filename of the image
    return $filename;
}

But when I run it, I get an Exception indicating that the system could not write to the path. And when I change the paths public_path() as follows:

    $users_image_location=public_path('images/users');
    $users_image_thumb_location_1=public_path('images/users/thumbs-100');
    $users_image_thumb_location_2=public_path('images/users/thumbs-50');
    $users_image_thumb_location_3=public_path('images/users/thumbs-30');

it works as expected.

I am running Laravel 5.7, PHP 7.2 on Windows 10 OS, Apache

I wish to know how I can store my images to storage folder instead of the public path.

I will appreciate any guide to getting this done Regards



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire