funny php infinate loop

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Drummer
Forum Newbie
Posts: 1
Joined: Mon Dec 13, 2004 2:42 pm

funny php infinate loop

Post by Drummer »

i am developing a zip program. that will take all the files inside a template folder. add them to a zip. but while doing this will change all filenames and entities of the filename, to 1.php,2.php, etc.

i designed the first version to run off of 2 arrays $Files, and $FileCount...

Code: Select all

$Files = array("Index.php", "Temp1.php", "Calendar2.php");
$FileCount = array("1.php","2.php","3.php");
this was fine.. until i was told the was going to be 250 files to be added to the zip file :evil:.

so i made a recursive loop that would check all the files inside the template folder. and make the arrays themselves.

this works well but when it goes to run the code... it does all the work. makes the zip. but doesn't load all the echo commands within the script (constantly loading). anyideas where the infinate loop is?

Code: Select all

<?
//Include files to add
include "Config.php";

function recursive_listdir($base) {
   static $filelist = array();
   static $dirlist = array();

   if(is_dir($base)) {
       $dh = opendir($base);
       while (false !== ($dir = readdir($dh))) {
           if (is_dir($base ."/". $dir) && $dir !== '.' && $dir !== '..') {
               $subbase = $base ."/". $dir;
               $dirlist[] = $subbase;
               $subdirlist = recursive_listdir($subbase);
           } elseif(is_file($base ."/". $dir) && $dir !== '.' && $dir !== '..') {
		   		$ext = pathinfo("$base/$dir"); 
				$ext = $ext['extension']; 
                if ($ext == "php" || $ext == "htm" || $ext == "tml" || $ext == "inc") {
			   		$filelist[] = $base ."/". $dir;
				}
           }
       }
       closedir($dh);
   }
   $array['dirs'] = $dirlist;
   $array['files'] = $filelist;
   return $array;
	

 }

//Get Template folder
if ($_REQUEST["Template"]) {
	$TempDir = $_REQUEST["Template"];
}

//Get Account Number
if ($_REQUEST["Account"]) {
	$Account = $_REQUEST["Account"];
}

//Get Directory Number
if ($_REQUEST["Dir"]) {
	$Dir = $_REQUEST["Dir"];
}

//Make Folder structure

//$DestFolder = "AcKram/Version1";

$DestFolder = $Account . "/" . $Dir;

//Makes the Account... THEN makes the "Dir" folder. Done in 2 steps, for ease of use.

//Check if account and Dir folder exists, make folder, if it doesn't
if(!is_dir($Account))
{
	mkdir($Account) or die ("Could not make directory - Account");
}

//Make Destination Folder
if(!is_dir($DestFolder))
{
	mkdir($DestFolder) or die ("Could not make directory - Dir");
}

$Files = recursive_listdir("./Test");

while(list($key,$val) = each($Files['files'])) { 
		$FileCount[] = ($key+1) . ".php";
} 

for($f=0; $f<count($Files['files']); $f++) {

	//Get Template FileName
	$TempFile = $Files['files'][$f]; //$TempDir . "/"  . $Files['files'][$f];
	
	//Get Destination FileName
	$DestFile = $DestFolder . "/"  . $FileCount[$f];
	
	//Load Template file into Array
	echo $TempFile;
		$arr=file($TempFile);
		
		//Clear previous text "UPDATED"
			$content = "";	
	//Go through every line of the file, replacing $Files and $Criteria
	//Loop text file and replace characters 
$i=0;

while($i < count($arr))
{
	$content .= str_replace($Files['files'][$f],$FileCount,$arr[$i]);
$i++;
} 
		//This is where it writes the new file in the Account/Dir folder
		
			   if (!$handle = fopen($DestFile, 'w')) {
					 $UpdateMessage = "Cannot open file ($DestFile)";
					 exit;
			   }
			
			   // Write $content to our opened file.
			   if (fwrite($handle, $content) === FALSE) {
				   $UpdateMessage = "Cannot write to file ($DestFile)";
				   exit;
			   }
			  
			   echo $DestFile . ' Updated with new information <a href="'. $DestFile . '">Open</a><BR>';
			  
			   fclose($handle);
			   
	
}
echo "<BR><BR>Finished File Creation<BR>Starting Archive";

//Get Zipping Class
//!!Ignore
require_once('pclzip.lib.php');

//Get FileName
//Makes the filename out of the $Dir (easy to change)
$file = $Account . "/" . $Dir . "$Dir.zip";

//Make Zip
$archive = new PclZip($file);
if ($archive->create($DestFolder) == 0) {
die('<BR>Error : '.$archive->errorInfo(true));
} else {
echo '<BR>Finished Archive <a href="' . $Account . "/" . $Dir . "/$Dir.zip" . '">Click to Download</a>';
}

//Moves Zip file into the Account/Dir . Instead of Account/
//This is at the end of the file, to stop the Zipping process, placing half the zip, inside itself (Don't ask!!)
  rename ($file, $Account . "/" . $Dir . "/$Dir.zip") or die ("Could not move file"); 

//FIN
?>
Drummer
User avatar
nielsene
DevNet Resident
Posts: 1834
Joined: Fri Aug 16, 2002 8:57 am
Location: Watertown, MA

Post by nielsene »

Is this the exact code your running? It seems like there is a missing '}' at least. The for loop looping over all the files appears to not be terminated. [OOPS my bad, I see it terminated now...]

I've tested your recursive_filelist and its working correctly, thought it could get confused by some symlinks I think -- that could cause an infinite loop.
Last edited by nielsene on Tue Jul 26, 2005 2:58 pm, edited 1 time in total.
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

Btw, you might want to have a look at the code snippets... I'm pretty sure there is a zip/unzip class...
Post Reply