E
E
Eugene2022-04-12 09:24:31
linux
Eugene, 2022-04-12 09:24:31

Why aren't files saved in the new subdirectory?

The very essence of the problem, if I save the image to a directory uploads/image.jpg- everything is fine, if I save it to an existing subdirectory uploads/images/image.jpg- it’s also fine, but when I try to save it to a new subdirectory, for example uploads/photos/image.jpg, an error occurs ... but if I create such a subdirectory on the server manually, then everything works ....

What could be the problem? Uploads has rights 755

ZY I use a package that calls the file_put_content function under the hood + I tried to create it manually via Storage::makeDirectory($path) - in vain...

Code:

public function setPhotoAttribute($photo)
    {
        $fileName = md5($photo->getClientOriginalName() . time()) . '.' . $photo->getClientOriginalExtension();

        foreach ($this->images as $folder) {
            $parameter = explode('x', $folder);
            $img = Image::make($photo);
            if (count($parameter) === 2) {
                [$width, $height] = $parameter;
                $img->resize($width, $height);
            }

            $path = Storage::path("photos/{$folder}/");
            if(!Storage::exists($path)){
                Storage::makeDirectory($path);
            }

            $img->save($path . $fileName);
        }

        $this->attributes['photo'] = $fileName;
    }

Answer the question

In order to leave comments, you need to log in

1 answer(s)
E
Eugene, 2022-04-12
@Kasperenysh

I solved the problem) I googled that the string Storage::path("photos/{$folder}/");returns an absolute path, but Storage::exists($path)takes the path relative to the project root.
This is how it worked:

$path = "photos/{$folder}/";
if(!Storage::exists($path)){
    Storage::makeDirectory($path);
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question