Ironmax's avatar

Storage::disk('images')->delete('image.png'); doesn't delete the file but deletes its existence.

Hi,

Storage::disk('images')->delete('image.png'); doesn't delete the file but delete its existence.

WHAT I MEAN BY DELETE ITS EXISTENCE

If I use dd(Storage::disk('images')->delete( $post->image )); once it returns true but if I use it again it returns false and its same for all files.

I upload the images like this:

$classifiedImg = $request->file('image');
$filename = Str::random(40) . '.png';
$image = Image::make($classifiedImg)
	->encode('png', 90)
    ->save(public_path('/storage/images/' . $filename));
Storage::disk('images')->put($filename, (string)$image);
$imagePath = $filename;

Then try deleting it like this:

if($post->image != '')
{
	$old_image_path = Storage::disk('images')->exists( $post->image );
    if($old_image_path == true)
    {
        Storage::disk('images')->delete( $post->image );
    }
}

Please help.

0 likes
21 replies
bobbybouwmann's avatar

All code in dd will actually be executed. The same goes for your delete call.

If you do dd(Storage::disk('images')->delete($post->image)); the image will be deleted right away. So the first time it will return true. The second time you hit this line, the image was already deleted so it makes sense it returns false because there was no image deleted.

Ironmax's avatar

But i can still see the image in the directory.

PLEASE NOTE: I initialized soft delete on its model. Does that affect it.

bobbybouwmann's avatar

The soft delete has nothing to do with the image itself. Only records in the database can be soft deleted, like your post model.

If you delete an image, it's actually deleted from the directory it's stored in. There is no default soft delete for files builtd in Laravel.

Ironmax's avatar

So please what do you think its causing this because it is only when i use File::delete() that's when it deletes the image.

bobbybouwmann's avatar

I'm not sure what the problem is? It deletes the file for you, right? That is what you want, right?

Ironmax's avatar

Yeah, but I have a feeling this will be troublesome for me in the future.

bobbybouwmann's avatar

Why? I honestly don't get your concern here. Can you make more clear what your concern is?

Ironmax's avatar

Wouldn't it be better to use this:

Storage::disk('images')->delete( $post->image );

Than this:

File::delete('storage/images/' . $post->image);
jlrdw's avatar

When you soft delete, it adds a timestamp to the deleted_at field.

But that's for the database record, I think if you delete (unlink) an image, it should be gone.

But reread the docs on soft deletes again.

Ironmax's avatar

Thanks for the info, but i don't want to use (unlink) to delete the images.

bobbybouwmann's avatar

Soft deletes have nothing to do with deleting a file from a system... Soft deletes are for models.

jlrdw's avatar

Laravel uses unlink through symfony/filesystem. Delete in laravel is unlink.

But concerning above, I can't see where you delete a database record, I only see where you delete an image.

Hard to tell why the image isn't deleted, as it should be.

Snapey's avatar

you are probably confused because you are saving the image twice in two different places then looking in one of them and deleting in the other

sort out your saving logic and you will have better results

$classifiedImg = $request->file('image');
$filename = Str::random(40) . '.png';
$image = Image::make($classifiedImg)
	->encode('png', 90)
    ->save(public_path('/storage/images/' . $filename));

// you just saved in the public folder

Storage::disk('images')->put($filename, (string)$image);

// now you saved in whatever you have specified as images disk

$imagePath = $filename;
Ironmax's avatar

Please, how do you mean twice.

All I did was:

Upon upload using Intervention\Image\Facades\Image; convert the image to a PNG format with a quality of 90, then save the image in a disk called images.

What am trying to achieve is that:

When a user uploads an image, the image should be converted to a PNG format then saved in a disk called images.

MichalOravec's avatar
Level 75

Remove save from your code.

->save(public_path('/storage/images/' . $filename));
Ironmax's avatar

Just tried this but the image ain't being uploaded, only the name gets submitted to the database.

MichalOravec's avatar

I don't know what is your images disk.

I would use public disk.

$image = Image::make($classifiedImg)->encode('png', 90);

Storage::disk('public')->put("images/{$filename}", (string) $image);
Ironmax's avatar

Just tried this and am getting same result.

Ironmax's avatar

Yeah, you were right all along.

AND AM REALLY SORRY TO HAVE WASTED YOUR TIME

Just discovered, it has been working the whole time but whenever it submits the image, it sends the image to:

storage\app\public\images meanwhile, it doesn't appear in public\storage\images.

So I had to run this command: php artisan storage:link, but it didn't link the storage directory because it already existed.

So I deleted the storage directory from the public directory, restarted the server and tried this command: php artisan storage:link one more time, then refreshed the page.

It worked.

I really thank you all for your time.

@bobbybouwmann @jlrdw @snapey @michaloravec

2 likes

Please or to participate in this conversation.