May 6, 200917 yr Hey guys, I am using the following script to delete files in different directories. The location of the directory is set by the posted value from the form before. However...the script isn't working. Can you please help me as I'm a beginner to PHP. Cheers! <?php $path = $_POST['list']; if(isset($_POST['file']) && is_array($_POST['file'])) { foreach($_POST['file'] as $file) { unlink($_POST['list'] . $file) or die("Failed to delete file"); } } ?> <form name="form1" method="post"> <?php $path = $_POST['list']; $dir_handle = @opendir($path) or die("Unable to open folder"); while (false !== ($file = readdir($dir_handle))) { if($file == "index.php") continue; if($file == ".") continue; if($file == "..") continue; echo "<input type='CHECKBOX' name='file[]' value='$file'>"; echo "<img src='$file' alt='$file'><br />"; } closedir($dir_handle); ?> <input type="submit" name="Delete" value="Delete"> </form>
May 6, 200917 yr what is $_POST['list']? Whats the value? This seems a bit overly complicated for my liking Josh. :S. Do you have the right permissions on said directory?
September 27, 200917 yr You should never give the user the ability to entirely wipe from the server, you should create a temp or "moved" files folder and place them there. It's for your protection in the long run. bool copy ( string $source , string $dest [, resource $context ] ) That is the short explanation of the copy function from PHP.net You could integrate this to your script like this possibly. $fileUserSelectedToDelete == $_POST['value']; $moveToThisDirectory == $_SERVER['DOCUMENT_ROOT'].'path/to/movedFilesFolder'; // Run the function copy($fileUserSelectedToDelete, $moveToThisDirectory, $output); if($output) return true; else return false; If you then want to "delete" multiple files at once you probably want to loop whilst a passed value remains true a while loop should see you well. Good luck
Create an account or sign in to comment