egiven's avatar

Delete file from dir with unlink

Hi, I am having a trouble with 'unlink' and it makes me crazy. I am uploading a video file (limited 1) and everything works fine. According to my system user can upload just 1 video. So if he/she upload second video, system overwrite new video to old video. But i always get error in my log file.

Version: 5.6.16

My controller

public function upload( \Illuminate\Http\Request $request, Emlak $emlak, $type = 'image') { if($request->hasFile('file')) { $file = $request->file('file'); if($type == 'image') { $emlakImage = $this->saveImage($emlak, $file); return [ 'emlak_image_id' => $emlakImage->id ];

        } elseif($type == 'video') {

            $md5Name = md5_file($request->file('file')->getRealPath() ) . time();
            $guessExtension = $request->file('file')->guessExtension();
            $emlakVideo = $file->storeAs('videos', $md5Name . '.mp4', 'public');

            $oldVideo = $emlak->video_url;
            $emlak->video_url = $emlakVideo;

            if($emlak->save()) {
                if(file_exists(public_path('/storage/') . $oldVideo));
                    unlink( public_path('/storage/') . $oldVideo);
            }

            return [
                'emlak_video_url' => $emlakVideo
            ];
        }
    }

    return false;
}

When i run the system (in my localhost windows 10 + xampp + php 7.1.20)

Log file error: unlink(C:\xampp71\htdocs\myproject\public\/storage/): Permission denied at C:\xampp71\htdocs\myproject\app\Http\Controllers\Admin\EmlakController.php:539)

Thank you.

0 likes
11 replies
tykus's avatar

Does your webserver user have write permission for the public/storage directory?

Any reason you are not using the Laravel FileSystem?

egiven's avatar

I have already write permission. As i said before i can able to upload video/photo properly. When i upload photo i am not getting any error. Only i got error when i upload video.

and this is my filesystem details. 'default' => env('FILESYSTEM_DRIVER', 'local'),

'disks' => [

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

    'public' => [
        'driver' => 'local',
        'root' => storage_path('app/public'),
        'url' => env('APP_URL').'/storage',
        'visibility' => 'public',
    ],

    's3' => [
        'driver' => 's3',
        'key' => env('AWS_KEY'),
        'secret' => env('AWS_SECRET'),
        'region' => env('AWS_REGION'),
        'bucket' => env('AWS_BUCKET'),
    ],

],
bestmomo's avatar

@tykus

Even the core doesn't use the FileSystem ^^

In UpCommand Console command :

@unlink(storage_path('framework/down'));
tykus's avatar

What's your point @bestmomo?

@egiven The error message does not show the filename you are attempting to unlink unlink(C:\xampp71\htdocs\myproject\public\/storage/). Are you sure that the path to the file is well-formed?

egiven's avatar
        $oldVideo = $emlak->video_url;
        $emlak->video_url = $emlakVideo;

        if($emlak->save()) {
            if(file_exists(public_path('/storage/') . $oldVideo));
                unlink( public_path('/storage/') . $oldVideo);
        }

are these wrong ?

tykus's avatar

Maybe $oldVideo is null and you are actually attempting to delete the public/storage/ directory because file_exists will return true for a directory; it might be worth putting a check in place!

$oldVideo = $emlak->video_url;
$emlak->video_url = $emlakVideo;

if($emlak->save()) {
    if($oldVideo && file_exists(public_path('/storage/') . $oldVideo));
        unlink( public_path('/storage/') . $oldVideo);
    }
}
egiven's avatar

Yes $oldVideo is null. By the way i already implemented your advice and the result is same.

tykus's avatar

the result is same

But, if $oldVideo is null then it should not attempt to unlink; have you resolved that?

egiven's avatar

Sorry, I don't understand what should i do now?

tykus's avatar

By the way i already implemented your advice and the result is same.

How are you getting the same result if $oldVideo is null? You should not call unlink; just for the sake of sanity you should not see "Sanity check: " output.

if($emlak->save()) {
    if($oldVideo && file_exists(public_path('/storage/') . $oldVideo));
    
        dd("Sanity check: " . $oldVideo);
    
        unlink( public_path('/storage/') . $oldVideo);
    }
}
egiven's avatar

I have solved the problem like that;

if($emlak->save()) { if($oldVideo !== null) { unlink( public_path('/storage/') . $oldVideo); } }

@tykus thank you so much for your help !

Please or to participate in this conversation.