Maxlight Maxlight - 3 months ago 7
PHP Question

creating image inside a folder results in creating an image with name folder/image.jpg

I have this piece of code:

$file = $faker->image($dir = public_path().'/tmp', $width = 800, $height = 600, '', true);
$hash = str_random(7);
$thumbnailName = $hash . '.jpg';
$thumbnailImage = ImgResizer::make($file)->fit(180, 180);
$thumbnailImage->save( public_path() . '\\thumb\\'. $thumbnailName);
rename($file, 'public/'.$hash.'.jpg');


As you can see I am using
faker
to populate the database. Faker supports images as well and it's getting images from lorempixel.com. I save this image directly in public folder and that works. I also create a thumbnail of the image using
InternventionImage
and save it to public/thumb folder as you can see in the code.

When I run the db:seed, no errors were produced, it all went well. However when I looked inside thumb folder there were no files there. There were bunch of images created inside public folder but not inside thumb. I logged in the server and cd into default folder and typed ls and I got this output:



As you can see on the image there are bunch of
public/thumb/hashcode.jpg
images there. How is this possible, they are not inside thumb folder, why is it listing them there? Did the script instead create file with filename
public/thumb/hashcode.jpg
? How do I put them inside thumb folder? This code works on my local machine windows 10 under apache2 and php7.

Answer

Try change this:

public_path() . '\\thumb\\'. $thumbnailName

To this:

public_path().DIRECTORY_SEPARATOR.'thumb'.DIRECTORY_SEPARATOR.$thumbnailName

Also, create thumb directory and set correct permissions:

chmod -R 755 /public/thumb
Comments