PHP Code Sample

<?php
//Append a piece of JavaScript to the bottom of every file in a hard-coded directory
//Assumes file is part of a dynamic system so it doesn't need to close HTML tag
//Minimal error checking since it's a quick utility script

$path = "/home/long/path/name/";

if ($handle = opendir($path)) {
    while (false !== ($file = readdir($handle))) {
        if ($file != "." && $file != "..")
        {
            $current = $path . $file;
            $trimmed = str_replace(".html", "", $file); //Removes ".html" since the JS is expecting that format
            echo ($trimmed. "\n<br />");	        
            $filer = fopen($current, "a");
            $info = "<script language=\"javascript\" >DoSomethingAJAXy('Server1','morepath/$trimmed');</script>\n";
            fwrite($filer, $info);
            fclose($filer);
        }
    } 
    closedir($handle);
}
?>

Back to Testbed