Skip to content

Instantly share code, notes, and snippets.

@kemo
Created March 23, 2012 13:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kemo/2170550 to your computer and use it in GitHub Desktop.
Save kemo/2170550 to your computer and use it in GitHub Desktop.
file::delete()
<?php
class file {
/**
* Error code to signal that the file is missing
* This is always a positive value because we can
* consider files which didn't exist already as 'deleted'.
*/
const MISSING = 1;
/**
* Deletes file(s) while gracefully handling "file doesn't exist" errors
*
* @author Kemal Delalic
* @param mixed $file path to file or array of paths
* @return mixed status or array of statuses
* @see http://php.net/unlink
*/
public static function delete($file)
{
// Remove arrays of files recursively
if (is_array($file))
{
$result = array();
foreach ($file as $key => $path)
{
$result[$path] = file::delete($path);
}
return $result;
}
try
{
if ( ! file_exists($file))
return file::MISSING;
return unlink($file);
}
catch (Exception $e)
{
return FALSE;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment