christa Posted November 23, 2009 Share Posted November 23, 2009 I must to delete before all the files in directories and after the directories. I use this code but it doesn't works: why? if ($handle = opendir("../$file_dir")) { while (false !== ($file = readdir($handle))) { if ($file != "." && $file != "..") { if(file_exists($file) && is_file($file)) { unlink($file); }elseif(is_dir($file)){ $handle = opendir($file); while (false !== ($files = readdir($handle))) { if(is_file($file.$files)){ unlink($file.$files); } } $handle = closedir($handle); rmdir($file); } } } closedir($handle); } Quote Link to comment https://forums.phpfreaks.com/topic/182692-remove-file-and-directory/ Share on other sites More sharing options...
c-o-d-e Posted November 23, 2009 Share Posted November 23, 2009 What exactly is the error you recieve, if there is one. If not, what does it do instead of what you're trying to do. Can you try tell me what you are trying to do, you're english isn't perfect. Quote Link to comment https://forums.phpfreaks.com/topic/182692-remove-file-and-directory/#findComment-964241 Share on other sites More sharing options...
christa Posted November 23, 2009 Author Share Posted November 23, 2009 simply, nothing happens: php returns no error and neither files nor folders are deleted. I want to do this operations: i have some directories (empty and not). By clicking a button on page, all directories (and files within ) must be deleted Quote Link to comment https://forums.phpfreaks.com/topic/182692-remove-file-and-directory/#findComment-964261 Share on other sites More sharing options...
Alex Posted November 23, 2009 Share Posted November 23, 2009 You are defining $file_dir, right? For debugging purposes you should put error_reporting(E_ALL); at the top of your page. Quote Link to comment https://forums.phpfreaks.com/topic/182692-remove-file-and-directory/#findComment-964269 Share on other sites More sharing options...
christa Posted November 23, 2009 Author Share Posted November 23, 2009 $file_dir is defined: his value is "../upload" error_reporting is already setted on E_ALL in php.ini Where i wrong? Quote Link to comment https://forums.phpfreaks.com/topic/182692-remove-file-and-directory/#findComment-964280 Share on other sites More sharing options...
Alex Posted November 23, 2009 Share Posted November 23, 2009 So the path to the directory that you want to remove is ../../upload, is that correct? If that path is correct and the problem lies somewhere else I'd suggest putting in debugging echos in varies parts of the code to see exactly what is and what isn't being executed, from that you should be able to determine what's going wrong. Quote Link to comment https://forums.phpfreaks.com/topic/182692-remove-file-and-directory/#findComment-964302 Share on other sites More sharing options...
christa Posted November 24, 2009 Author Share Posted November 24, 2009 now, i added else at the end: the script go to else printing the directories names!. if ($handle = opendir("../$file_dir")) { while (false !== ($file = readdir($handle))) { if ($file != "." && $file != "..") { if(file_exists($file) && is_file($file)) { unlink($file); }elseif(is_dir($file)){ $handle = opendir($file); while (false !== ($files = readdir($handle))) { if(is_file($file.$files)){ unlink($file.$files); } } $handle = closedir($handle); rmdir($file); } else { echo $file ."<br>"; } } } } closedir($handle); Quote Link to comment https://forums.phpfreaks.com/topic/182692-remove-file-and-directory/#findComment-964561 Share on other sites More sharing options...
c-o-d-e Posted November 24, 2009 Share Posted November 24, 2009 Im not too sure if this would be the best and easiest way. If the directories are placed under a database, wouldn't it be easier to delete all the rows using a mysql query or by deleting the row of the logged in user? Quote Link to comment https://forums.phpfreaks.com/topic/182692-remove-file-and-directory/#findComment-964593 Share on other sites More sharing options...
christa Posted November 24, 2009 Author Share Posted November 24, 2009 Im not too sure if this would be the best and easiest way. If the directories are placed under a database, wouldn't it be easier to delete all the rows using a mysql query or by deleting the row of the logged in user? the directories are not placed under a database. I don't understand why nor file nor directories are deleted Quote Link to comment https://forums.phpfreaks.com/topic/182692-remove-file-and-directory/#findComment-964596 Share on other sites More sharing options...
c-o-d-e Posted November 24, 2009 Share Posted November 24, 2009 In that case, how does a directory get added? you'd pretty much reverse it. I'm not much help, never done this before. Sorry. Quote Link to comment https://forums.phpfreaks.com/topic/182692-remove-file-and-directory/#findComment-964603 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.