PHP Find and Replace in File


<?php
echo <<<A
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

<head>
<meta http-equiv="Content-Language" content="en-us" />
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Dir</title>
</head>

<body>
A;
$dir        = !empty($_POST['dir'])? $_POST['dir'] : $_GET['dir'];
$replace    $_POST['replace'];
$with       $_POST['with'];
$submit     $_POST['submit'];

if(
is_dir($dir) && !empty($with) && !empty($replace)){

    
$files    scandir($dir);
    
    foreach(
$files as $filename){
        if(
strlen(basename($filename".php")) < strlen($filename)){
            
$handle    = @fopen($dir."/".$filename"rb");
            
$contents  = @fread($handlefilesize($dir."/".$filename));
            
fclose($handle);
 
            
//$content    = str_replace($replace, $with, $contents);
            
$content    preg_replace("/$replace/"$with$contents);
            
$fh            = @fopen($dir."/".$filename,"wb");
        
            
fwrite($fh,$content);
            
fclose($fh);
        }    
    }
}


echo >>>
A
<form method="post" action="" style="text-align:center">
<
center>
<
table style="font-family: Tahoma; font-size: 13px;" cellspacing="3" cellpadding="3">
    <
tr>
        <
td style="width: 60px; height: 24px; text-align: left;" valign="top">
        
Dir:</td>
        <
td style="height: 24px; text-align: left;" valign="top">
        <
input name="dir" type="text" style="width: 274px" />&nbsp;</td>
    </
tr>
    <
tr>
        <
td style="width: 60px; height: 24px; text-align: left;" valign="top">
        
Replace:</td>
        <
td style="height: 24px; text-align: left;" valign="top">
        <
input name="replace" type="text" style="width: 274px" /></td>
    </
tr>
    <
tr>
        <
td style="width: 60px; height: 24px; text-align: left;" valign="top">
        
With:</td>
        <
td style="height: 24px; text-align: left;" valign="top">
        <
input name="with" type="text" style="width: 274px" /></td>
    </
tr>
    <
tr>
        <
td style="width: 60px; height: 24px; text-align: left;" valign="top">&nbsp;</td>
        <
td style="height: 24px; text-align: center;" valign="top">
        <
input name="submit" type="submit" value="submit" style="width: 66px" />&nbsp;</td>
    </
tr>
    <
tr>
        <
td style="height: 24px; text-align: left;" valign="top" colspan="2">
A;
echo 
"<pre>";
/*
if(is_dir($dir)){

    $dirs  = array_map("realpath", scandir($dir));
    foreach($dirs as $dir){
        echo '<a href="?dir='.$dir.'">'.$dir.'</a>'."\n";
    }

}

if (is_dir($dir)) {
    if ($dh = opendir($dir)) {
        while (($file = readdir($dh)) !== false) {
            echo '<a href="?dir='.$dir . $file.'">'.$dir . $file.'</a>'."\n";
        }
        closedir($dh);
    }
}
*/
if(is_dir($dir)){
 if (
$handle opendir($dir)) {
    while (
false !== ($file readdir($handle))) {
        
//if ($file != "." && $file != "..") {
        
            
$path  realpath($dir ."/"$file);
            
$path  str_replace("\\""/"$path);
            
$path  str_replace("//""/"$path);
            
            echo 
'<a href="?dir='.$path.'">'.$path.'</a>'."\n";
        
//}
    
}
    
closedir($handle);
 }
}
echo 
"</pre>";
echo <<<A
</td>
    </tr>
</table>
</center>
</form>
</body>

</html>
A;
?>