skip to Main Content

I have a case suppose I upload an image for a product to a temporary directory called "uploads" and then later move it to another one called "products" and the image will be in a sub-directory which created dynamically like this

products/1/variants/1/image.jpeg
  1. "products" is the main directory have permission 0755 which is accessible
  2. "/1/variants/1/" dynamically created directory have permission 0700 which is not accessible and can’t read the image

my code of moving

$sourcePath = 'public/'.$image['path'];
$destinationPath = "products/".$product>id."/variants/".$variantImages['variantId']."/".pathinfo($sourcePath, PATHINFO_BASENAME);


Storage::makeDirectory("public/images/products/".$product->id."/variants/".$variantImages['variantId']);

Storage::move($sourcePath, "public/images/".$destinationPath);

notice : I use Hostinger hosting

image moved to the right directory but i can’t read because of the permission

I expect to read the image

  1. storage:link (symbolic link) is correct
  2. filesystem config for storage disk is correct and set to 0755
  3. I also tried to use storage visibility Storage::setVisibility()
  4. when using Storage::makeDirectory($dir, 0755, true)

2

Answers


  1. I think what you are trying to do is move images from the storage path to the public folder.

    Storage::put('file.jpg', $contents, 'public');
    

    I think this is more the approach you should go. Either that or change the visibility if you want it persisted.

    $visibility = Storage::getVisibility('file.jpg');
     
    Storage::setVisibility('file.jpg', 'public');
    

    the public folder being accessible and then you can add the path you would like it to be found in. Same goes for the put.

    if not and you are truly wanting to do what you are doing above and fix it. You have an error in question where you have "$product>id" and it should be "$product->id"

    Login or Signup to reply.
  2. You have to check your user/group of your webserver, hopefully you have access to ssh and make necessary change.

    $ sudo chown www-data:www-data -R storage/path
    $ sudo chmod 755 -r storage/path
    

    now you should be able to read the created image.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search