Created
March 23, 2012 13:19
-
-
Save kemo/2170550 to your computer and use it in GitHub Desktop.
file::delete()
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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