mattnewark's avatar

Uploading image to the storage folder

Hi Guys,

I am trying to upload images to the storage folder that is located above the public_html folder on my server so that I am able to hide my .env file etc.

So what I am doing is creating the directory in the storage folder which is working fine:

if(!Storage::exists("/public/images/$userName"))
        {
            Storage::makeDirectory("/public/images/$userName");
        } 
        if(!Storage::exists("/public/images/$userName/$auditTitle"))
        {
            Storage::makeDirectory("/public/images/$userName/$auditTitle");
        }    
        if(!Storage::exists("/public/images/$userName/$auditTitle/$date"))
        {
            Storage::makeDirectory("/public/images/$userName/$auditTitle/$date");
        }
        $fullPath = "/public/images/$userName/$auditTitle/$date";
return $fullPath;

Once this is done it is passed to the queue where it is processed and added to the file structure and uploaded to the DB:

public function handle()
    {
            if ($this->auditimage['Base64Image'])
            {
                Log::info('Adding New image ');
                $mime = explode(';', $this->auditimage['Base64Image']); 
                $mime1 = explode(':', $mime[0]);
                $type = explode('/', $mime1[1]); 

                $image = Image::make($this->auditimage['Base64Image'])->resize(1000, 1000, function ($constraint) {
                    $constraint->aspectRatio();});
                    Storage::putFileAs($fullPath, $image, $filename);
                    $image->destroy(); 

I have passed the fullpath and the filename from the controllers.

When I check the file structure it has created the directories with no issues but does not add the image to it and then in DB in the failed jobs table I get:

ErrorException: file_put_contents(/home/custzone/auditstagingcore/storage/app/public/images/Matthew_Morris/Title_Dan/2017-12-20): failed to open stream: Is a directory in /home/custzone/auditstagingcore/vendor/league/flysystem/src/Adapter/Local.php:198

Any help would be appreciated to get this to work.

0 likes
9 replies
RamjithAp's avatar

You need to use storage_path() helper to declare $fullPath.

if(!Storage::exists("/public/images/$userName"))
        {
            Storage::makeDirectory("/public/images/$userName");
        } 
        if(!Storage::exists("/public/images/$userName/$auditTitle"))
        {
            Storage::makeDirectory("/public/images/$userName/$auditTitle");
        }    
        if(!Storage::exists("/public/images/$userName/$auditTitle/$date"))
        {
            Storage::makeDirectory("/public/images/$userName/$auditTitle/$date");
        }
        $fullPath = storage_path("/public/images/".$userName."/".$auditTitle."/".$date);
        return $fullPath;
mattnewark's avatar

Hi @RamjithAp

Thank you for answer.

So my structure for the site is:

Root auditstagingcore app public_html auditstaging public images

What I would like to do is create the folder structure in the auditstaging->public->images folder and also save the images to it. for the life of me I am unable to find a way to to do this as everything relates to the auditstagingcore.

What would I need to do to the below to create the folders in the auditstaging->public->images folder:

if(!Storage::exists("/public/images/$userName"))
        {
            Storage::makeDirectory("/public/images/$userName");
        } 
        if(!Storage::exists("/public/images/$userName/$auditTitle"))
        {
            Storage::makeDirectory("/public/images/$userName/$auditTitle");
        }    
        if(!Storage::exists("/public/images/$userName/$auditTitle/$date"))
        {
            Storage::makeDirectory("/public/images/$userName/$auditTitle/$date");
        }
        $fullPath = "/public/images/$userName/$auditTitle/$date";
        return $fullPath;

and in turn what would I need to do to save the image to the above created folder in the below code:

$image = Image::make($this->auditimage['Base64Image'])->resize(1000, 1000, function ($constraint) {
                    $constraint->aspectRatio();})->save(storage_path().$this->fullPath.'/'.$this->filename, 50);
                    $image->destroy(); 

Thanks

RamjithAp's avatar

Alright, here is the solution.

 if(!File::exists(public_path("/images/".$userName."/".$auditTitle."/".$date))
        {
            File::makeDirectory(public_path("/images/".$userName."/".$auditTitle."/".$date));
        }
 $fullPath = public_path("/images/".$userName."/".$auditTitle."/".$date);
 return $fullPath;

And save

$image = Image::make($this->auditimage['Base64Image'])
                    ->resize(1000, 1000, function ($constraint) {
                    $constraint->aspectRatio();})
                    ->save($fullPath.'/'.$this->filename);
                    $image->destroy(); 
mattnewark's avatar

Hi @RamjithAp,

Thank you for the help, I was originally using File:: but the error I get is:

"message": "mkdir(): No such file or directory",
    "exception": "ErrorException",
    "file": "/home/custzone/auditstagingcore/vendor/laravel/framework/src/Illuminate/Filesystem/Filesystem.php",

Do you have any idea why this does not work?

Thanks

mattnewark's avatar

Hi @RamjithAp,

I am now able to create the directories in the public folder by using the below:

if(!File::exists($_SERVER['DOCUMENT_ROOT']."/images/".$userName))
        {
            File::makeDirectory($_SERVER['DOCUMENT_ROOT']."/images/".$userName,0777,true);
        } 
        if(!File::exists($_SERVER['DOCUMENT_ROOT']."/images/".$userName."/".$auditTitle));
        {
            File::makeDirectory($_SERVER['DOCUMENT_ROOT']."/images/".$userName."/".$auditTitle,0777,true);
        }    
        if(!File::exists($_SERVER['DOCUMENT_ROOT']."/images/".$userName."/".$auditTitle."/".$date))
        {
            File::makeDirectory($_SERVER['DOCUMENT_ROOT']."/images/".$userName."/".$auditTitle."/".$date,0777,true);
        }

but when I try and save the image it is using the below url:

/home/custzone/auditcore/public//home/custzone/public_html/auditstaging/public/images/Title_Dan/2017-12-20/image_pUignBpKso.png

using this code:

$image = Image::make($this->auditimage['Base64Image'])
                    ->resize(1000, 1000, function ($constraint) {
                    $constraint->aspectRatio();})
                    ->save($fullPath.'/'.$this->filename);
                    $image->destroy();

I am assuming that the first part /home/custzone/auditcore/public/ is what is created using the save method, is there another method that I am able to use that does automatically add a directory structure?

Thanks

RamjithAp's avatar

Try this

$image = Image::make($this->auditimage['Base64Image'])
                    ->resize(1000, 1000, function ($constraint) {
                    $constraint->aspectRatio();})
                    ->save(public_path("/images/".$userName."/".$auditTitle."/".$date."/").$this->filename);
                    $image->destroy();
mattnewark's avatar

Hi @Snapey

Could you please help me, I followed your help guide on moving the app folders out of the public_html folder and now I am having troubles getting it save images to the storage folder or the public folder that is inside the public_folder..

Thanks

mattnewark's avatar

Hi,

I have fixed the issue, It was a queue process issue that was causing it to not save in the correct place.

Please or to participate in this conversation.