Web EndMemo

PHP Delete File


unlink() function can be used to delete a file.
<?PHP
	unlink('test.txt');
?>

Since system() executes commands, it can be used for delete files. For windows:
<?PHP
	$delfile = "del \"test.txt\"";
	system($delfile);
?>

For Linux:
<?PHP
	$delfile = "rm \"test.txt\"";
	system($delfile);
?>

rmdir() function can delete an empty directory.
<?PHP
	if (is_dir("dir"))
	{
	   rmdir("dir");
	}
?>

Following PHP code can delete all files and subdirectories in a directory recursively.
<?PHP
  function deldir($dir)
  {
	if (is_dir($dir))
	{
	   $targ = scandir($dir);
	   foreach ($targ as $item)
	   {
		  if ($item != "." && $item != "..")
		  {
			  $item = $dir . "/" . $item;
			  if (filetype($item) == "dir")
				  deldir($item);
			  else
				  unlink($item);
		  }
	   }
	   rmdir($dir); //delete the empty directory finally
	}
  }
?>