2

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 the following Exception indicating that the system could not write to the path.

Can't write image data to path (C:\xampp\htdocs\NelpritadConcept\storage\images/users/NITA5VYtiWGSbWLmy2M6mtTpbW6XyupA.jpeg)

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

Josh
  • 1,660
  • 5
  • 33
  • 55
  • Can you please try pass blank in local 'local' => [ 'driver' => 'local', 'root' => storage_path(''), ] ? – Firoz Tennali Feb 22 '19 at 12:36
  • @FirozTennali, Do you mean for public disc or for users disc? – Josh Feb 22 '19 at 12:43
  • It's for local and try. – Firoz Tennali Feb 22 '19 at 12:45
  • Not working still – Josh Feb 22 '19 at 12:57
  • @FirozTennali Not working still. However it did not throw the Exception this time but the images were not created/uploaded to the expected locations. But since it did not throw up any Exceptions, I am assuming it might have dropped them somewhere else but I cant seem to find them anywhere within the application directories even the public folder. Is there any other thing you think might get us closer to a solution? – Josh Feb 22 '19 at 22:20
  • Check your storage folder. – Firoz Tennali Feb 23 '19 at 06:31
  • @FirozTennali It is not there in the storage folder – Josh Feb 23 '19 at 19:22

0 Answers0