1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
|
<?
/**
* Delete the provided directory and all its contents from the FTP-server.
*
* @param string $path Path to the directory on the FTP-server relative to the current working directory
*/
function DeleteDirRecursive ($resource, $path)
{
$list = ftp_nlist ($resource, $path);
if ( empty($nlist) ){
$list = RawlistToNlist ( ftp_rawlist($resource, $path), $path . ( substr($path, strlen($path) - 1, 1) == "/" ? "" : "/" ) );
}
if ($list[0] != $path){
$path .= ( substr($path, strlen($path)-1, 1) == "/" ? "" : "/" );
foreach ($list as $item){
if ($item != $path.".." && $item != $path."."){
DeleteDirRecursive ($item);
}
}
ftp_rmdir ($resource, $path);
}
else {
ftp_delete ($resource, $path);
}
}
/**
* Convert a result from ftp_rawlist() to a result of ftp_nlist()
*
* @param array $rawlist Result from ftp_rawlist();
* @param string $path Path to the directory on the FTP-server relative to the current working directory
* @return array An array with the paths of the files in the directory
*/
function RawlistToNlist ($rawlist, $path)
{
$array = array();
foreach ($rawlist as $item){
$filename = trim(substr($item, 55, strlen($item) - 55));
if ($filename != "." || $filename != ".."){
$array[] = $path . $filename;
}
}
return $array;
}
?> |
Partager