bizerk Posted February 17, 2007 Share Posted February 17, 2007 Alright well here is what I have for the remove ALL FILES of a directory: <? //delete ALL FILES $name = $_POST['name']; $password = $_POST['pass']; if($name == "admin" && $password == "admin") { $dirname = "/files/"; if (!is_dir($dirname)) return false; $dscan = array(realpath($dirname)); $darr = array(); while (!empty($dscan)) { $dcur = array_pop($dscan); $darr[] = $dcur; if ($d=opendir($dcur)) { while ($f=readdir($d)) { if ($f=='.' || $f=='..') continue; $f=$dcur.'/'.$f; if (is_dir($f)) $dscan[] = $f; else unlink($f); } closedir($d); } } $i_until = ($only_empty)? 1 : 0; for ($i=count($darr)-1; $i>=$i_until; $i--) { echo "\nDeleting '".$darr[$i]."' ... "; if (rmdir($darr[$i])) echo "ok"; else echo "FAIL"; } return (($only_empty)? (count(scandir)<=2) : (!is_dir($dirname))); }else{ echo "Do not have the correct Privlages"; } ?> It does not make any errors, but it dosen't do anything either :/. Here is what I used to delete specific files. Let's say for example ".txt" files. <? //delete all jpgs $name = $_POST['name']; $password = $_POST['pass']; if($name == "admin" && $password == "admin") { foreach (glob("/files/*.txt") as $filename) { echo "$filename size " . filesize($filename) . "\n"; unlink($filename); } }else{ echo "You do not have the Right Privlages to Delete any Files"; ?> but these both do not seem to be workingg :/. Any better suggestions or previosley written code so I can delete all files in a directory and SPECIFIC files like certain extensions? I could not get recursive deleting to work either... Link to comment https://forums.phpfreaks.com/topic/38840-removing-all-the-files-of-a-certain-directory-and-specific-files-from-a-directo/ Share on other sites More sharing options...
sspoke Posted February 17, 2007 Share Posted February 17, 2007 does echo "$filename size " . filesize($filename) . "\n"; display the correct filename if it does unlink($filename); deletes the filename in the current folder.. i think u need unlink("\files\".$filename); opps edit unlink("/files/".$filename); Link to comment https://forums.phpfreaks.com/topic/38840-removing-all-the-files-of-a-certain-directory-and-specific-files-from-a-directo/#findComment-186758 Share on other sites More sharing options...
printf Posted February 17, 2007 Share Posted February 17, 2007 array_map... <?php // remove all array_map ( 'unlink', glob ( './junk/*' ) ); // remove a certain type array_map ( 'unlink', glob ( './junk/*.txt' ) ); ?> printf Link to comment https://forums.phpfreaks.com/topic/38840-removing-all-the-files-of-a-certain-directory-and-specific-files-from-a-directo/#findComment-186762 Share on other sites More sharing options...
bizerk Posted February 17, 2007 Author Share Posted February 17, 2007 Okay thanks guys, I got everything to work here is what I ended up using for the delete ALL code: <? $name = $_POST['name']; $password = $_POST['password']; if($name == "admin" && $password == "examplepass") { $dir = "files/"; $msg = ""; function deldir($dir) { if ($handle = opendir($dir)) { while (false !== ($file = readdir($handle))) { if ($file != "." && $file != "..") unlink($dir.$file); } closedir($handle); } echo "Directory has been deleted"; } $result =deldir($dir); { #create output message if ($result == true) { $msg = "Done!"; } } }else{ echo "Not a Admin Account click <a href='index.php'>here</a> to go back"; } ?> and the array_map... I am not quite understanding what you want me to do... here is what i have right now for the txt.php file: <? //delete all txts $name = $_POST['name2']; $password = $_POST['pass2']; if($name == "admin" && $password == "pass123"){ array_map ( 'unlink', glob ( './files/*.txt' ) ); }else{ echo "You do not have the Right Privlages to Delete any Files"; } ?> is that supposed to work, because it is not working correctly...:/ any other suggestions? Link to comment https://forums.phpfreaks.com/topic/38840-removing-all-the-files-of-a-certain-directory-and-specific-files-from-a-directo/#findComment-186778 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.