Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Mdp85's avatar

Delete uploaded file from public dir

Hi guys, i'm just trying to use File::delete to delete file from public path, but always i got the message "File not found at path". Any suggestions ?

0 likes
17 replies
bobbybouwmann's avatar

Show the code you use? We can't help you if can't see what you are doing!

davidfaux's avatar

You could use

unlink(public_path('file/to/delete'));
//use @unlink if you want to prevent generating an error if the file does not exist
Mdp85's avatar

This is the code: i injected the filesystem contract in the construct and then, i use $this->file->delete method, but it seems that doesn't works:

return $this->file->delete(
        public_path() . DIRECTORY_SEPARATOR . $model->medias->first()->path . DIRECTORY_SEPARATOR .         $model->medias->first()->filename
);
Mdp85's avatar

@dfaux with unlink works fine, but i don't know why with file->delete doesn't. Maybe i must setting some configuration with filesystem ?

davidfaux's avatar
Level 7

@Mdp85 You sure you have the file's path correct? It is usually as simple as:

// Delete a single file
File::delete($filename);

// Delete multiple files
File::delete($file1, $file2, $file3);

// Delete an array of files
$files = array($file1, $file2);
File::delete($files);

//Example above from: http://laravel-recipes.com/recipes/133/deleting-a-file
//To lazy to type out myself :)
1 like
Mdp85's avatar

@dfaux yes the path is correct, and i put on browser the string that

public_path() . DIRECTORY_SEPARATOR . $model->medias->first()->path . DIRECTORY_SEPARATOR .         $model->medias->first()->filename

return, and the file is opened correctly. Mistery of Filesystem!! :)

SteveAzz's avatar

@Mdp85 There is no need to call the 'public_path()' function if you are using the

File::delete()

If the file is loacted inside of the images all you need to do is

File::delete('images/' . 'test.jpg');
1 like
SteveAzz's avatar

@Mdp85 What error is it giving you when you do that? Where is the file you wish to deleted located at ?

Mdp85's avatar

I said it in the first post:

Hi guys, i'm just trying to use File::delete to delete file from public path, but always i got the message "File not found at path". Any suggestions ?

the dir files is public/documents.

Mdp85's avatar

@Colossus ok man, i'll tried later. Thanks all !! I'm so embarrassed, my bad, you right! Thanks again!!

I notice that works with facade, but with Filesystem o factory file doesn't works. Why ?

SteveAzz's avatar

I think it is because it starts from a different directory, don't quote me on that tho. Also make sure you mark one of these answers correctly just in case someone in the future is having the same problem as you are.

Glad to be of help!

Mdp85's avatar

when I find the real problem, I will mark the answer as correct. For now i don't know the real reason! ;)

Sorry and thanks all, i found the problem, I had to change the public.path, and now works fine. Thanks all again!

Luca.Filosofi's avatar

Hi all,

i guess the problem is that you have defined and using your own App\File class and this will conflict some way with the built in File facade...

try doing something like this:

// you have this
use App\File;
// add this
use Illuminate\Support\Facades\File as LaraFile;

class FileController extends Controller
{
  public function destroy($id) {
        LaraFile::delete("path/{$filename}");
    }
}
1 like
Sareth's avatar

Place "use File;" on the top of the controller file and then use the following code inside you function.

File::delete(public_path().'if file path inside public folder');

File::delete(app_path.'if file path inside app folder');

Hope it help! :)

Please or to participate in this conversation.