funny php infinate loop
Posted: Sun Jul 24, 2005 6:22 pm
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...
this was fine.. until i was told the was going to be 250 files to be added to the zip file
.
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?
Drummer
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");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
?>