Friday, February 26, 2016

Delete a file in Laravel 5.1

Delete file in Laravel 5.1:

Today we will discuss how to delete a file in Laravel 5.1

You know you could use PHP's unlink() method, but want to do it the Laravel way then use the File::delete() method.

<?php

namespace App\Http\Controllers;

use File;

class TestController extends Controller
{

 public function deleteFile()
 {
  // 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);
 }

}
?>


Errors are quietly ignored.

If there's a problem deleting the file, any error is silently ignored. If it's important that a file was deleted, check it's existence after deleting it with File::exists().

Thanks.

No comments:

Post a Comment