westman Posted March 16, 2015 Share Posted March 16, 2015 Hi all, I am trying to remove a folder if it exists and files within it if they exists. I keep getting this error. Fatal error: Cannot redeclare delete_directory() (previously declared in /home/content/31/1234321/html/ads/index.php:236) in /home/content/1234321/html/ads/index.php on line 236 $time04 = date("Y-m-d",time() - (60*60*24*7*26)); // 6.5 mouths (26 weeks)$time04 = mysql_real_escape_string($time04);$sql02_info_get = mysql_query("SELECT * FROM ads WHERE live='0' AND addate < TIMESTAMP('$time04')");$sql02_info_get_count = mysql_num_rows($sql02_info_get);if ($sql02_info_get_count > 0){while($row = mysql_fetch_array($sql02_info_get)){$remove_adid = $row["adid"];$directory = "pics/$remove_adid";//if(!is_dir($directory)){//echo "good";//exit(); //}else{//echo "bad";//exit(); //}function delete_directory($directory, $empty=false) // line 236{ if(substr($directory,-1) == DIRECTORY_SEPARATOR){ $directory = substr($directory,0,-1); } if(!is_dir($directory) || !is_readable($directory)){ return false; } else{ $handle = opendir($directory); while (false !== ($item = readdir($handle))){ if($item != '.' && $item != '..'){ $path = $directory.DIRECTORY_SEPARATOR.$item; if(is_dir($path)){ delete_directory($path); } else{ unlink($path); } } } closedir($handle); if($empty == false){ if(!rmdir($directory)){ return false; } } return true; }} delete_directory($directory);$remove_adid = mysql_real_escape_string($remove_adid);$sql02 = mysql_query("DELETE FROM ads WHERE adid='$remove_adid'"); } // end loop} any help? Link to comment Share on other sites More sharing options...
justsomeguy Posted March 16, 2015 Share Posted March 16, 2015 You put the delete_direction function definition inside a while loop. That means that each time through the loop it tries to redefine the same function. The error happens on the second time through the loop because it was already defined the first time through the loop. Link to comment Share on other sites More sharing options...
westman Posted March 16, 2015 Author Share Posted March 16, 2015 omg you are right. the loop is messing up the second time. I can see it works 1 time since i have one less entry in my DB. now then.... how do i make it work on a loop? Link to comment Share on other sites More sharing options...
justsomeguy Posted March 16, 2015 Share Posted March 16, 2015 Remove the function definition from the loop. There's no reason to define a function in a loop. Link to comment Share on other sites More sharing options...
westman Posted March 16, 2015 Author Share Posted March 16, 2015 do you mean the last line? delete_directory($directory); Link to comment Share on other sites More sharing options...
justsomeguy Posted March 16, 2015 Share Posted March 16, 2015 That's the line where you execute the function. You need to remove the part where you define the function. It's fine to execute a function in a loop, but you can only define it once. Link to comment Share on other sites More sharing options...
westman Posted March 16, 2015 Author Share Posted March 16, 2015 (edited) Do you mean this line? function delete_directory($directory, $empty=false) // line 236{ If not please copy and past the line so it is clear. Edited March 16, 2015 by westman Link to comment Share on other sites More sharing options...
justsomeguy Posted March 16, 2015 Share Posted March 16, 2015 It's not one line, it's the entire block of code that defines that function. That's the line where it starts. Link to comment Share on other sites More sharing options...
westman Posted March 16, 2015 Author Share Posted March 16, 2015 how am I going to remove the code in the block that defines the function and still have a function than functions? I do not understand. please be more clear by telling me all the key words I need to remove or paste some code to see. Link to comment Share on other sites More sharing options...
justsomeguy Posted March 16, 2015 Share Posted March 16, 2015 You don't need to define a function right before you use it. You can define it at any point. If you put the function definition outside of any other structures like an if statement or loop, even if you put the function definition at the end of the file, then it will be defined when the code runs. When PHP runs a script it will first go through and define any global functions, then it will go through again and actually execute the code. The only time it does not define a function before executing all of the code is if you put the function definition inside something like an if statement or loop. It's common to put all function definitions at the beginning or end of a file. I usually put them at the end. Link to comment Share on other sites More sharing options...
westman Posted March 16, 2015 Author Share Posted March 16, 2015 I do not know what part of my code defines the function so here is what I did... $time04 = date("Y-m-d",time() - (60*60*24*7*26)); // 6.5 mouths (26 weeks)$time04 = mysql_real_escape_string($time04);function delete_directory($directory, $empty=false){ if(substr($directory,-1) == DIRECTORY_SEPARATOR){ $directory = substr($directory,0,-1); } if(!is_dir($directory) || !is_readable($directory)){ return false; } else{ $handle = opendir($directory); while (false !== ($item = readdir($handle))){ if($item != '.' && $item != '..'){ $path = $directory.DIRECTORY_SEPARATOR.$item; if(is_dir($path)){ delete_directory($path); } else{ unlink($path); } } } closedir($handle); if($empty == false){ if(!rmdir($directory)){ return false; } } return true; }} $sql02_info_get = mysql_query("SELECT * FROM ads WHERE live='0' AND addate < TIMESTAMP('$time04')");$sql02_info_get_count = mysql_num_rows($sql02_info_get);if ($sql02_info_get_count > 0){while($row = mysql_fetch_array($sql02_info_get)){$remove_adid = $row["adid"];$directory = "pics/$remove_adid";//if(!is_dir($directory)){//echo "good";//exit(); //}else{//echo "bad";//exit(); //}delete_directory($directory);$remove_adid = mysql_real_escape_string($remove_adid);$sql02 = mysql_query("DELETE FROM ads WHERE adid='$remove_adid'"); } // end loop} will this work? if not please give code examples. Link to comment Share on other sites More sharing options...
justsomeguy Posted March 16, 2015 Share Posted March 16, 2015 Did you test it? I do not know what part of my code defines the functionWhy not? Are you using someone else's code? You'll need to understand what the code is doing in order to effectively change it. Link to comment Share on other sites More sharing options...
westman Posted March 16, 2015 Author Share Posted March 16, 2015 (edited) thank you it works. you got me! it is not my code but I have used it before and it has worked outside a loop. would love to know how each line of the code works. have you got the time? Edited March 16, 2015 by westman Link to comment Share on other sites More sharing options...
justsomeguy Posted March 16, 2015 Share Posted March 16, 2015 I can help fill in the details, but you can look up each individual function there to see what it does. The only thing that might confuse you is DIRECTORY_SEPARATOR, which is a constant that holds the character to separate directories ("" on Windows, "/" on Unix). Other than that though, check the manual if you see a function you don't understand, e.g.:http://php.net/manual/en/function.substr.phphttp://php.net/manual/en/function.is-dir.phphttp://php.net/manual/en/function.is-readable.phphttp://php.net/manual/en/function.opendir.phphttp://php.net/manual/en/function.readdir.phpLike I said, if you have questions about specific parts I can help fill in the details. Start with the manual though and see what you can work out. Link to comment Share on other sites More sharing options...
westman Posted March 17, 2015 Author Share Posted March 17, 2015 thank you. Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now