Laravel 5.6 - Permission denied when trying to retrieve image


Laravel 5.6 - Permission denied when trying to retrieve image



I have a Laravel 5.6 project running on a Windows Bitnami WAMP stack. I am storing an image like this..


$imageName = myfile.jpg
$path = $request->file->storeAs('images', $imageName);



This works and the file is correctly stored, I am trying to retrieve it like this..



$source = Storage::get('images', $imageName);



But I get the error message...


file_get_contents(C:Bitnamiwampstack-7.1.18-1apache2htdocsmyprojectstorageappimages): failed to open stream: Permission denied



This looks like a permissions issue, the storageappimages folder looks like this


drwxr-xr-x 1 win_user 192142 0 Jul 2 18:15 images



Anyone any ideas where I am going wrong?





Make sure the whole storage folder has the correct permissions
– Vilius
Jul 2 at 17:36


storage





Have confirmed 0755 permissions on storage folder and all subfolders
– fightstarr20
Jul 2 at 17:59





755 is supposed to be for folders and 644 for files
– Vilius
Jul 2 at 18:01


755


644





Correct, both are set correctly
– fightstarr20
Jul 2 at 18:09





Who owns the file (image) that gets stored?
– J. Arbet
Jul 2 at 18:40




2 Answers
2



file_get_contents(C:Bitnamiwampstack-7.1.18-1apache2htdocsmyprojectstorageappimages): failed to open stream: Permission denied



you are save the file to default local disk, check your config/filesystems.php



default local disk is not allow access by url


'disks' => [

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

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



oops, my mistake, you are get the file, not display the image by url



try $source = Storage::get('images/' . $imageName);


$source = Storage::get('images/' . $imageName);





I have checked the filesystems.php and it looks exactly as above
– fightstarr20
Jul 2 at 18:51





try $source = Storage::get('images/' . $imageName);
– kenken9999
Jul 2 at 18:59



It's permission issue. Execute the following commands in the parent directory of your document root.



Please note that public_html is not a public directory of the laravel project. It's the parent directory.


public_html


public



For Ubuntu:


sudo chown -R www-data:www-data public_html



For centOS


sudo chown -R apache:apache public_html



In case your web server does not have permissions to write a file, executing these commands will solve the issue. Let me know if it helps.






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

PHP contact form sending but not receiving emails

PHP parse/syntax errors; and how to solve them?