Jump to content

remove a folder and files


westman

Recommended Posts

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

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

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

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

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

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 by westman
Link to comment
Share on other sites

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

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...