deevo's avatar

Trying to Delete Local File with Storage Facade

All,

So, I am working on a little project that involves some image manipulation / upload to S3 / Deletion of Local Copy, etc

Here is where I am: I can submit the file dropzone, manipulate it with intervention image, save a local copy, upload that file, persist the record. The problem comes when I try to delete the local copy. I know this code is a little sloppy for now, I am just trying to get it to work how I want before refactoring. I just have a fileupload method on my controller as shown below:

public function addPhoto($id, ImageUploadRequest $request, Factory $filesystem){
        $file = $request->file('image');
        $fileName = time() . $file->getClientOriginalName();
        $tempFilePath = public_path('images/temp/') . $fileName;
        $image = Image::make($file->getRealPath())->resize(300, null, function ($constraint) {
            $constraint->aspectRatio();
        })->orientate()->save($tempFilePath);

        $uploadSuccess = $filesystem->put('spots-images/' . $fileName, $image->__toString(), 'public');
        if($uploadSuccess):
            $photo = SpotImage::create(['spot_id' => $id, 'image' => $fileName, 'image_thumbnail' => 'tn-' . $fileName]);
            Storage::delete($tempFilePath); //FAILING HERE!  Remove this line and everything works perfectly
            return Response::json(['filename' => $fileName], 200);
        endif;
    }

I am getting a League\Flysystem\FileNotFoundException

It is looking for the file at the

$tempFilePath = public_path('images/temp/') . $fileName;

That is definitely where the file is located because that is where I save it to. The reason I have to save the file is because I cannot use the $filesystem->put method without saving the file (Maybe this is the real problem and I need to re-think how I am preparing the file before upload)

Anyway, any help would be greatly apprecaited.

0 likes
4 replies
d3xt3r's avatar

The filenames are resolved relative to the root folder defined in config. Change it accordingly

 'local' => [
            'driver' => 'local',
            'root'   => storage_path('app'),
        ],
deevo's avatar

I am not sure I understand. The Storage facade is looking in the right place. This is in fact exactly where the file is located:

 <span class="exception_title"><abbr title="League\Flysystem\FileNotFoundException">FileNotFoundException</abbr> in <a title="/home/vagrant/Sites/kiteros/vendor/league/flysystem/src/Filesystem.php line 381" ondblclick="var f=this.innerHTML;this.innerHTML=this.title;this.title=f;">Filesystem.php line 381</a>:</span>
<span class="exception_message">File not found at path: home/vagrant/Sites/kiteros/public/images/temp/1455902109deevoweb-logo.png</span>

I am not an amazing programmer, just trying to build something. So, if my questions seem dumb, please take it easy on me!

d3xt3r's avatar
d3xt3r
Best Answer
Level 29

Yups, the message is confusing. Its showing you the filename you passed it as $tempFilePath. But when its trying to delete its actually looking at home/vagrant/Sites/kiteros/storage/app/home/vagrant/Sites/kiteros/public/images/temp/1455902109deevoweb-logo.png. I recall someone else facing this problem.

Just try this out, place something as storage/app/test.png and then try deleting just by passing $tempFilePath = 'test.png'. Hope that will make sense.

1 like
deevo's avatar

I get it. I didn't realize that the storage path was going to the storage folder. For whatever reason I though it was going to the app folder at the root of the project (not the app folder of the storage directory in the root of the project.)

You were most helpful. Thank you so much!

Please or to participate in this conversation.