Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Sure, you'd have to use <code>unlink()</code> and <code>rmdir()</code>, and you'd need a recursive directory removal function because <code>rmdir()</code> doesn't work on directories with files in them. You'd also want to make sure that the deletion script is really secure to stop people from just deleting everything.</p> <p>Something like this for the recursive function:</p> <pre><code>function Remove_Dir($dir) { $error = array(); if(is_dir($dir)) { $files = scandir($dir); //scandir() returns an array of all files/directories in the directory foreach($files as $file) { $fullpath = $dir . "/" . $file; if($file == '..' || $file == '.') { continue; //Skip if ".." or "." } elseif(is_dir($fullpath)) { Remove_Dir($fullpath); //recursively remove nested directories if directory } elseif(is_file($fullpath)) { unlink($fullpath); //Delete file otherwise } else { $error[] = 'Error on ' . $fullpath . '. Not Directory or File.' //Should be impossible error, because everything in a directory should be a file or directory, or . or .., and thus should be covered. } } $files = scandir($dir); //Check directory again if(count($files) &gt; 2) //if $files contains more than . and .. { Remove_Dir($dir); } else { rmdir($dir); //Remove directory once all files/directories are removed from within it. } if(count($error) != 0) {return $error;} else {return true;} } } </code></pre> <p>Then you just need to pass the file or directory to be deleted through GET or something to the script, probably require <code>urlencode()</code> or something for that, make sure that it's an authorized user with permissions to delete trying to delete the stuff, and <code>unlink()</code> if it's a file, and <code>Remove_Dir()</code> if it's a directory.</p> <p>You should have to prepend the full path to the directory or file to the directory/file in the script before removing the directory/file.</p> <p>Some things you'll want for security is firstly making sure that the deletion is taking place in the place it's supposed to, so someone can't do <code>?dir=/</code> or something and attempt to delete the entire filesystem from root, which can probably be circumvented by prepending the appropriate path onto the input with something like <code>$dir = '/home/user/public_html/directories/' . $_GET['dir'];</code>, of course then they can potentially delete everything in that path, which means that you need to make sure that the user is authorized to do so.</p> <p>Need to keep periodic backups of files just in case.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload