Scanning a Directory For PHP Errors

My fel­low web geeks might find this script, php-check down­load home on the range movie , use­ful. It recur­sive­ly scans a direc­to­ry check­ing PHP files for syn­tax errors.

Just copy it some­where in your path (like /usr/local/bin) and chmod it to 755.

I wrote the script because I edit PHP using Notepad++, so it’s easy for small typos to enter my scripts. I need­ed a quick way to scan a direc­to­ry after upload­ing revised files.
I wrote it in PHP so that those who need it will also know how to cus­tomize it.

[php]
#!/usr/bin/php
?php // php-check ver­sion 1.0 // recur­sive­ly scans a direc­to­ry for .php files and runs php ‑l on // them (php ‑l checks for PHP syn­tax errors) // revi­sions at http://glenandpaula.com/wordpress/archives/2007/10/31/scanning-a-directory-for-php-errors/ if (php_sapi_name()!=‘cli’) { die(“This util­i­ty can only be run from the com­mand line.\n”); } $counter=0; $errors=false; func­tion scan_dir($dir) { $counter=0; $dh=opendir($dir); while ($file=readdir($dh)) { if ($file==’.’ || $file==’..’) con­tin­ue; if (is_dir($dir.’/’.$file)) { $counter+=scan_dir($dir.’/’.$file); } else { if (substr($file, strlen($file) — 4) == ‘.php’) { $counter++; $output=shell_exec(“/usr/bin/php ‑l $dir/$file 2>&1”);
if (substr($output,0,2)!=‘No’) { // skips the “No syn­tax errors in …” mes­sage
$errors=true;
echo $out­put;
}
}

}
}
return $counter;
}
if ($argc!=2) {
die(“Usage: php-check dirname (usu­al­ly php-check .)\n”);
}

if (!is_dir($argv[1])) {
die(“Argument must be a direc­to­ry. The most com­mon usage is php-check .\n”);
}

$counter=scan_dir($argv[1]);

echo “$counter files checked\n”;
exit($errors);
?>
[/php]

This is a quick and dirty script — there are prob­a­bly some bugs in it. User beware.

If you find it help­ful, you might also want to check out scripts like PHP CodeSnif­fer

or PHP Beau­ti­fi­er.down­load robot chick­en star wars dvd

2,780 thoughts on “Scanning a Directory For PHP Errors”

Leave a Reply