Issue with file edit in PHP using file_put_contents (Laravel)

296 Views Asked by At

Good afternoon everyone, I have run across a weird issue which I can't put my finger on and I am hoping somebody can help me figure out what is causing this problem. To provide some context, I allow the user to store an array of images on a product, and after they are stored using laravel-stapler package which is configured in the following way:

 public function __construct(array $attributes = [])
{
    parent::__construct($attributes);

    $this->hasAttachedFile('image', [
        'styles' => [
            'thumbnail' => '500x500#',
            'large' => '800x800#'
        ],
        'url' => '/media/image/:id/:style/:filename',
        'default_url' => '/img/category-placeholder-greyscale.jpg',
        'convert_options' => [
            'jpeg_quality' => 60
        ]
    ]);
}

The images are saved in three folders:
../path-to-image/original/file-name
../path-to-image/thumbnail/file-name
../path-to-image/large/file-name

After I save these images I use croppie.js to rotate and edit them. After the user edits and crops them and they submit it the image is sent as a base64 to a controller and the controller is shortened below to only the relevant parts:

$imageData = $request->get('imagebase64');

list(, $imageData) = explode(';', $imageData);
list(, $imageData) = explode(',', $imageData);
$imageData = base64_decode($imageData);
// $image is loaded up through dependency injection
$path = public_path('/path' . '/large/' . $image->image_file_name);
$path2 = public_path('/path' . '/original/' . $image->image_file_name);


file_put_contents($path, $imageData);
file_put_contents($path2, $imageData);

This works on my local machine just fine, the image is saved in both folders and I get a new cropped and edited image, but on my server this doesn't work, the first file_put_contents doesn't work and doesn't store a new image into the /large folder but the second file_put_contents works and stores a new image into the /original folder.

I am not sure why does this happen and would appreciate any help you can give me. I also do not think it is due to permissions because I gave the folder for the images the right permission but I can't be certain. The code doesn't crash also it just executes without saving the first image
1

There are 1 best solutions below

0
On

you might have to edit the .htaccess file to give permissions to these files