BangularTK
BangularTK

Reputation: 554

Laravel Delete File

I have a test.txt file in my Laravel public folder which I simply want to delete from my controller by using

unlink('public/test.txt');

Whenever I try to run, I keep getting a 'file not found' error

ErrorException in WidgetController.php line 32: unlink('public/test.txt'): No such file or directory

What seems to be the problem here?

Upvotes: 4

Views: 11054

Answers (2)

Maryam Homayouni
Maryam Homayouni

Reputation: 943

The public_path function returns the fully qualified path to the public directory and the base_path function returns the fully qualified path to the project root.

So if you want to reach a file in public folder you can use public_path() like this:

unlink(public_path("test.txt"));

You can also use File:delete instead of unlink like this:

use File;
File::delete(public_path("test.txt"));

Upvotes: 1

S.I.
S.I.

Reputation: 3375

Use app_path to delete the file. More about the paths here

$file_path = app_path("test.txt"); // app_path("public/test.txt");
if(File::exists($file_path)) File::delete($file_path);

Upvotes: 3

Related Questions