Giacomo M
Giacomo M

Reputation: 4723

Storage::delete not deleting the file. The path is correct and the file permissions are correct

I do not know why I am not able to delete a file in Laravel with the code:

$path = storage_path('app/identification_cards') . '/' . $filename;
Storage::delete($path)

The command is executed without errors and it returns true.

What I checked:
- the path is correct. If I use the same exact path in a bash terminal (with the "rm" command) the file is deleted;
- the file does have 777 permissions.

I don't know how to solve it.

Thanks.

Upvotes: 4

Views: 3360

Answers (2)

CodeToLife
CodeToLife

Reputation: 4171

use Illuminate\Support\Facades\File instead of Storage. The chunk of code working on my localhost right now:

    $separatorLcl=DIRECTORY_SEPARATOR;// a '\' on win os, '/' on linux or whatever
    $image = $request->file('userprofile_picture');
    $filename = time() . '.' . $image->getClientOriginalExtension();
    try {
        Image::make($image)->resize(300, 300)->save( storage_path('app'
            .$separatorLcl.'public'.$separatorLcl.'rasmho'.$separatorLcl. $filename ) );
    }catch ( \Exception $e){
        l::k('fayli soxta nashud');
    }
    if($request->hasFile('userprofile_picture')){
        l::k('$user2='.$user->name);//logging

        $photo=$user->photo;
        if(is_null($photo)){
            $user->photo()->create([
                'path'=>storage_path('app'
                    .$separatorLcl.'public'.$separatorLcl.'rasmho'.$separatorLcl. $filename )
            ]);
        }
        else{
            l::k($photo->path);//logging
            try {
                File::delete($photo->path);
            }catch ( \Exception $e){
                l::k('fayli photo nest');
            }
            $photo->path=storage_path('app'
                .$separatorLcl.'public'.$separatorLcl.'rasmho'.$separatorLcl. $filename );
            $photo->save();
        }

Upvotes: 0

Vision Coderz
Vision Coderz

Reputation: 8078

Storage::delete will point to storage\app\ path so no need to add app folder name once again

  Storage::delete('identification_cards/'.$filename);

Upvotes: 7

Related Questions