Hello all. I am trying to read a directory file by file with a while statement. I want to strip out all javascript contained in the page.
Here is my script below. I have two problems with it. First, it only reads one file at random and then stops. Second, it doesnt strip out the javascript. I tried reading the file and then searching for a string start and end, and striping out what the search found.
Any help is great!
$onsite_dir = "/home/XXX/public_html/XXX/";
$onsite_url = "http://www.XXX.com/XXX/";
$Start = "<script type=\"text/javascript\" language='javascript'>";
$GrabEnd = "</script>";
$open = opendir($onsite_dir);
while (($check = readdir($open))!== false){
if ($check != "." && $check != ".." && (is_dir($check) !== TRUE)) {
$handle = fopen($check, "r");
if (!function_exists('file_get_contents')) {
$read = fread($handle, 800000);
} else {
$read = file_get_contents($onsite_url.$check);
}
$read = str_replace("$Start(.*)$GrabEnd", "", $read);
if(!chmod($onsite_dir.$check, 0755)){ echo "Cannot change permission on file."; exit;}
if(!$open = fopen($onsite_dir.$check, "w")) { echo "Error :: Cannot open file: ".$check; exit;}
if (!fwrite($open, $read)) { echo "Cannot write to file: ".$handle."<br>"; exit;}
echo $check." Success.";
break;
}
}
Thanks!
Thanks!