Skip to content

Commit 46f206b

Browse files
committed
Bugfix: Image not store when give path is not writable
- Make method makeDirectoryIfDoesNotExist in ImageHandler and call when store method call
1 parent ff1bbb4 commit 46f206b

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

src/ImageHandler.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Codepane\LaravelImageHandler;
44

55
use Exception;
6+
use Illuminate\Support\Facades\File;
67
use Illuminate\Support\Facades\Storage;
78
use Intervention\Image\ImageManager;
89

@@ -30,6 +31,8 @@ public function store(object $image, string $imgPath = null): string
3031
/* Creating a file name for the original image. */
3132
$fileOriginalName = $this->makeFileName($imgPath, $time, 'orig');
3233

34+
$this->makeDirectoryIfDoesNotExist($storageDisk, $fileOriginalName);
35+
3336
$this->storeImg($storageDisk, $fileOriginalName, file_get_contents($image));
3437

3538
foreach($dimensions as $key => $dimension) {
@@ -47,6 +50,24 @@ public function store(object $image, string $imgPath = null): string
4750
return $fileOriginalName;
4851
}
4952

53+
/**
54+
* make a directory if does not exist
55+
*
56+
* @param string $storageDisk
57+
* @param string $fileName
58+
*
59+
* @return void
60+
*/
61+
public function makeDirectoryIfDoesNotExist(string $storageDisk, string $fileName)
62+
{
63+
$directory = pathinfo($fileName, PATHINFO_DIRNAME);
64+
65+
if(($storageDisk == 'local' || $storageDisk == 'public') && !Storage::disk($storageDisk)->exists($directory)) {
66+
$dirPath = Storage::disk($storageDisk)->path($directory);
67+
File::makeDirectory($dirPath, 0777);
68+
}
69+
}
70+
5071
/**
5172
* It stores an image in a specified disk.
5273
*

0 commit comments

Comments
 (0)