Deleting a File

Problem

You want to delete a file on the filesystem.

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

Solution

Use the File::delete() method.

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

Discussion

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().

comments powered by Disqus