Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel Delete File

Tags:

php

laravel-5

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?

like image 432
BangularTK Avatar asked Oct 31 '25 02:10

BangularTK


1 Answers

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);
like image 194
S.I. Avatar answered Nov 01 '25 18:11

S.I.



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!