Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to delete file from public folder in laravel 5.1

Tags:

laravel

People also ask

How do I delete public files in Laravel?

You could use PHP's unlink() method just as @Khan suggested. But if you want to do it the Laravel way, use the File::delete() method instead. $files = array($file1, $file2); File::delete($files);

How do I delete files from Storage folder in Laravel?

One way to delete a file from the public directory in Laravel is to use the Storage facade. To delete a file, you will need to follow the following steps: Step 1: Check to ensure that the folder and file exist. Step 2: Delete the required file.

How do I delete files after downloading in Laravel?

Just place it in the controller instead of the app/start/global. php." DeleteFileAfterSend(true) works great on Laravel 5.3 as well. Although the documentation doesn't state anything about it, you can still use it.


You could use PHP's unlink() method just as @Khan suggested.

But if you want to do it the Laravel way, use the File::delete() method instead.

// 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);

And don't forget to add at the top:

use Illuminate\Support\Facades\File; 

Use the unlink function of php, just pass exact path to your file to unlink function :

unlink($file_path);

Do not forget to create complete path of your file if it is not stored in the DB. e.g

$file_path = app_path().'/images/news/'.$news->photo;

this method worked for me
First, put the line below at the beginning of your controller:

use File;

below namespace in your php file Second:

 $destinationPath = 'your_path';
 File::delete($destinationPath.'/your_file');

$destinationPath --> the folder inside folder public.


First, you should go to config/filesystems.php and set 'root' => public_path() like so:

'disks' => [

    'local' => [
        'driver' => 'local',
        'root' => public_path(),
],

Then, you can use Storage::delete($filename);


Using PHP unlink() function, will have the file deleted

$path = public_path()."/uploads/".$from_db->image_name;
unlink($path);

The above will delete an image returned by $from_db->image_name located at public/uploads folder


Try to use:

unlink('.'.Storage::url($news->photo));

Look the dot and concatenation before the call of facade Storage.