Jump to content

Script fails on Windows but works on Unix


dlcmpls

Recommended Posts

Hi All.  I have a rather simple Find/Replace script that I use on a couple of large sites.

 

The script works flawlessly on Unix machines, but when I try to run it on a Windows machine, the script fails. When I run the script on Windows, the feedback tells me the script ran, but when I look at the files, nothing happened.

 

Can anyone tell me why?

 

My script is below and attached.  Note that I hard coded the variable to be Found and Replaced for testing/simplicity.  The variables can be found on lines 74 and 75.

 

Note that the script is exactly the same on both server OS's except for line 83 which is the path.

 

Any help is greatly appreciated.

 

------------------

 

<html>
<head>
<title>Batch string find and replace</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8895">
<link rel="stylesheet" href="image/style.css" type="text/css">
</head>

<body bgcolor="#FFFFFF" text="#000000">
<table width="500" border="0" cellspacing="0" cellpadding="0" align="center" background="">
  <tr> 
    <td> 
	  <div align="center"><B>Result of batch content replace</B></div>
    </td>
  </tr>				
    </table>
<br>
<table width="500" border="0" cellspacing="0" cellpadding="0" align="center">
  <tr> 
	<td bgcolor="#6B4D44"> 
	  <table width="500" border="0" cellspacing="1" cellpadding="0">
		<tr> 
		  <td bgcolor="#FDF6EB"> <br>
			<table width="470" border="0" cellspacing="0" cellpadding="3" align="center">
			  <tr> 
				<td width="470">
<?
//For vertion 4.2 or higher
extract($_POST);
extract($_GET);

$checkbox_num="3";
function get_dir_and_file($dir)
{
static $data     = array();
$file           = '';
$handle         = '';
$full_file_name = '';			

if ( $handle = opendir( $dir ) )
{    
	while( false != ( $file = readdir( $handle ) ) )
	{ 
		if ( $file != '.' && $file != '..' )
		{
			$full_file_name = $dir . '/' . $file;

			if ( filetype( $full_file_name ) != 'dir' )
				$data[] = $full_file_name;

			if ( filetype( $full_file_name ) == 'dir' )
				get_dir_and_file( $full_file_name );							
		}
	}

	closedir( $handle );
}
return $data;
}

function checkbox_select($num,$array)
{
for($i=0;$i<$num;$i++)
{
	if($array[$i])
	{
		$index=$array[$i];
		$Field[$index]=1;
	}
}
return $Field;
}

////////  BEGIN SCRIPT MOD BY DLC.  051106   MOD'S MADE SO THIS SCRIPT CAN RUN STANDALONE, WITHOUT USER INPUT.
		$word1 = 'head' ;
		$word2 = 'myhead' ;
		$directory = 'y' ;
		$postfix[0] ='htm' ;
		$postfix[1] ='html' ;
		$postfix[2] ='php' ;
//			$allsuffix = 'y' ;

//////// THIS PATH MUST BE SET FOR EACH SERVER INDIVIDUALLY!  MOVE THIS LINE TO THE FILE ON THE CLIENT SERVER!!!!
		$path = '/web/root/www/what/' ;
////////  END MOD BY DLC

function check_file($filename,$word1,$word2,$bigflg,$backupflg,$postfixarray,$allsuffix)
{
if($allsuffix)
{
	$Fp=fopen($filename,"r");
	$Contents=fread($Fp,filesize($filename));
	fclose($Fp);
	$word2=stripslashes($word2);
	$word1=stripslashes($word1);
////////  REMOVED BY DAVE.  051106.  THESE 2 LINES REMOVED LINE BREAKS AND SCREWED UP THE HTML LAYOUT.
	//$word1=ereg_replace("\r","",$word1);
	//$Contents=ereg_replace("\r","",$Contents);
	$Contents=str_replace("|","\##",$Contents);
	$word1=str_replace("|","\##",$word1);
	$word2=str_replace("|","\##",$word2);
	$word1=quotemeta($word1);
	if($bigflg)
		$pos=ereg($word1, $Contents);
	else
		$pos=eregi($word1, $Contents);		
	if($pos)
	{
		if($backupflg)
		{
			$new_filename=$filename.".bak";
			$wfp=fopen("$new_filename","w+");
			fputs($wfp,$Contents);
			fclose($wfp);
		}

		if($bigflg)
			$new_Contents=ereg_replace($word1,$word2,$Contents);
		else
			$new_Contents=eregi_replace($word1,$word2,$Contents);

		$new_Contents=str_replace("\##","|",$new_Contents);
		$wfp=fopen("$filename","w+");
		fputs($wfp,$new_Contents);
		fclose($wfp);
		return $filename;
	}
	else
		return false;
}	
elseif($postfixarray)
{
	$pstr=strrchr(basename($filename),".");
	$pstr=substr($pstr,1,(strlen($pstr)-1));
	if($postfixarray[$pstr]==1)
	{
		$Fp=fopen($filename,"r");
		$Contents=fread($Fp,filesize($filename));
		fclose($Fp);
		$word2=stripslashes($word2);
		$word1=stripslashes($word1);
////////  REMOVED BY DAVE.  051106.  THESE 2 LINES REMOVED LINE BREAKS AND SCREWED UP THE HTML LAYOUT.
		//$word1=ereg_replace("\r","",$word1);
		//$Contents=ereg_replace("\r","",$Contents);
		$Contents=str_replace("|","\##",$Contents);
		$word1=str_replace("|","\##",$word1);
		$word2=str_replace("|","\##",$word2);

		$word1=quotemeta($word1);
		if($bigflg)
			$pos=ereg($word1, $Contents);
		else
			$pos=eregi($word1, $Contents);
		if($pos)
		{
			if($backupflg)
			{
				$new_filename=$filename.".bak";
				$wfp=fopen("$new_filename","w+");
				fputs($wfp,$Contents);
				fclose($wfp);
			}

			if($bigflg)
				$new_Contents=ereg_replace($word1,$word2,$Contents);
			else
				$new_Contents=eregi_replace($word1,$word2,$Contents);

			$new_Contents=str_replace("\##","|",$new_Contents);
			$wfp=fopen("$filename","w+");
			fputs($wfp,$new_Contents);
			fclose($wfp);
			return $filename;
		}
		else
			return false;
	}
	else
		return false;
}

}
set_time_limit(0);
echo "Begin time=".date("H:i:s")."<br>";
if(isset($postfix))
{
$totalarray=checkbox_select($checkbox_num,$postfix);
$totalarray_num=count($totalarray);
if($selfpostfix)
{
	$sarray=explode(",", $selfpostfix);
	for($i=0;$i<count($sarray);$i++)
	{
		$index=$sarray[$i];
		$totalarray[$index]=1;
	}
}
}
elseif($selfpostfix)
{
$sarray=explode(",", $selfpostfix);
for($i=0;$i<count($sarray);$i++)
{
	$index=$sarray[$i];
	$totalarray[$index]=1;
}
}
$realnum=0;
if($directory)
{
$dirdata=get_dir_and_file($path);
$dirnum=count($dirdata);
for($i=0;$i<$dirnum;$i++)
{
	if(file_exists($dirdata[$i])&&$dirdata[$i]!="index.php"&&$dirdata[$i]!="reok.php")
	{
		$file1=check_file($dirdata[$i],$word1,$word2,$big,$backup,$totalarray,$allsuffix);
		if($file1)
		{
			$realnum++;
			echo $dirdata[$i]."..........<font color=red>replaced ok!</font><br>";
		}
		else
			echo $dirdata[$i]."..........pass!<br>";
	}
}
}
else
{
$dirnum=0;
$handle=@opendir($path);
while ($file = readdir($handle)) {
	if($file!="."&&$file!="..")
	{
		$filepath=$path."/".$file;			
		if(file_exists($filepath)&&is_file($filepath)&&$filepath!="index.php"&&$filepath!="reok.php")
		{
			$file1=check_file($filepath,$word1,$word2,$big,$backup,$totalarray,$allsuffix);
			if($file1)
			{
				$realnum++;
				echo $filepath."..........<font color=red>replaced ok!</font><br>";
			}
			else
				echo $filepath."..........pass!<br>";
			$dirnum++;
		}
	}
}
}

echo "End time=".date("H:i:s")."<br>";
echo "Total file=$dirnum<br>";
echo "Replaced file=$realnum<br>";



////////  BEGIN SENDING THE EMAIL
			$domain = $_SERVER['HTTP_HOST']; 
			$clientemail = "[email protected]" ; 
			$rundate = date("l dS of F Y h:i:s A"); 
			$subject = "Successful - $domain";				
////////  SET THE MESSAGE CONTENT FOR THE CONFIRMATION EMAIL
			$messagecontent = "Process performed and successful for:<br><br><strong>$domain</strong><br><br>
				on $rundate.<br><br><br>					
				<B>Result of process:</B><br><br>
						<blockquote>
						Total files Processed = $dirnum<br><br>
						Updated files = $realnum<br><br>
						File Path = $path<br><br>
						</blockquote>
						";
			$headers† = 'MIME-Version: 1.0' . "\n"; 	
			$headers .= "To: $clientemail" . "\n"; 	
			$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\n"; 				// Additional headers
			$headers .= "From: [email protected]" . "\n"; 
							//$headers .= 'Cc: [email protected]' . "\n";
							//$headers .= 'Bcc: [email protected]' . "\n"; 
							// Mail it 
			mail($to, $subject, $messagecontent, $headers); 
?> 

				</td>					
			  </tr>				  
			</table>				
			<br>
		  </td>
		</tr>
	  </table>
	</td>
  </tr>
</table>



</body>
</html>

 

-----------------

 

[attachment deleted by admin]

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.