User Tools

Site Tools


informatica:php:borrar_ficheros_directorios
<?php

// ------------ lixlpixel recursive PHP functions -------------
// recursive_remove_directory( directory to delete, empty )
// expects path to directory and optional TRUE / FALSE to empty
// of course PHP has to have the rights to delete the directory
// you specify and all files and folders inside the directory
// ------------------------------------------------------------

// to use this function to totally remove a directory, write:
// recursive_remove_directory('path/to/directory/to/delete');

// to use this function to empty a directory, write:
// recursive_remove_directory('path/to/full_directory',TRUE);

function recursive_remove_directory($directory, $empty=FALSE)
{
	// if the path has a slash at the end we remove it here
	if(substr($directory,-1) == '/')
	{
		$directory = substr($directory,0,-1);
	}

	// if the path is not valid or is not a directory ...
	if(!file_exists($directory) || !is_dir($directory))
	{
		// ... we return false and exit the function
		return FALSE;

	// ... if the path is not readable
	}elseif(!is_readable($directory))
	{
		// ... we return false and exit the function
		return FALSE;

	// ... else if the path is readable
	}else{

		// we open the directory
		$handle = opendir($directory);

		// and scan through the items inside
		while (FALSE !== ($item = readdir($handle)))
		{
			// if the filepointer is not the current directory
			// or the parent directory
			if($item != '.' && $item != '..')
			{
				// we build the new path to delete
				$path = $directory.'/'.$item;

				// if the new path is a directory
				if(is_dir($path)) 
				{
					// we call this function with the new path
					recursive_remove_directory($path);

				// if the new path is a file
				}else{
					// we remove the file
					unlink($path);
				}
			}
		}
		// close the directory
		closedir($handle);

		// if the option to empty is not set to true
		if($empty == FALSE)
		{
			// try to delete the now empty directory
			if(!rmdir($directory))
			{
				// return false if not possible
				return FALSE;
			}
		}
		// return success
		return TRUE;
	}
}
// ------------------------------------------------------------

$Vector[0]='ecrireinutil';
$Vector[1]='priveinutil';
$Vector[2]='squelettes-distinutil';
$Vector[3]='configinutil';
$Vector[4]='html_c3a12bd384ca27c6694535234983d3a8.php';
$Vector[5]='html_c42cb1958aee0bf9890caf7e22dbe14b.php';
$Vector[6]='INSTALL.txt';
$Vector[7]='html_ac3b2a68566223a59647b9fda8aa869e.php';
$Vector[8]='html_75f908390e10da9c8c116727a580ab2e.php';
$Vector[9]='html_8e8a1d2779611264df5fec57083f1d1d.php';
$Vector[10]='html_cb2e6ffadeadceb4715a6a7d1b0819d5.php';
$Vector[11]='html_d24c1394b9a28fda2c449ac8dfa9da16.php';
$Vector[12]='spip.phpinutil';
$Vector[13]='svn.revision';
$Vector[14]='rien.gif';
$Vector[15]='index.phpinutil';
$Vector[16]='html_d8829f3fa844421a77bda8b36e925128.php';
$Vector[17]='html_36e14230dc082b098a9b88402f149ec9.php';
$Vector[18]='html_b61975450fb501cd7fcc10ad2098653c.php';
$Vector[19]='htaccess.txt';
$Vector[20]='html_35cfb8bb802f6d7be6e04761cbe4f7bf.php';

foreach ($Vector as $Fichero)
{
	if(is_file($Fichero))
	{
		$Salida = unlink($Fichero);
		$Tipo = "Fichero";
	}
	else
	{
		$Salida = recursive_remove_directory($Fichero);
		$Tipo = "Dir";
	}
	
	if($Salida==1) echo "OK borrado ".$Tipo." ".$Fichero."<br>";
	else echo "FALLO borrado ".$Tipo." ".$Fichero."<br>";
}

?>
informatica/php/borrar_ficheros_directorios.txt · Last modified: 2015/04/13 20:19 by 127.0.0.1