disciple, husband, father, college minister, blowhard
RSS icon Home icon
  • Scanning a Directory For PHP Errors

    Posted on October 31st, 2007 Geek No comments

    My fellow web geeks might find this script, php-check download home on the range movie , useful. It recursively scans a directory checking PHP files for syntax errors.

    Just copy it somewhere 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 needed a quick way to scan a directory after uploading revised files.
    I wrote it in PHP so that those who need it will also know how to customize it.

    PHP:
    1. #!/usr/bin/php
    2. <?php
    3. // php-check version 1.0
    4. // recursively scans a directory for .php files and runs php –l on
    5. // them (php –l checks for PHP syntax errors)
    6. // revisions at http://glenandpaula.com/wordpress/archives/2007/10/31/scanning-a-directory-for-php-errors/
    7.  
    8. if (php_sapi_name()!=‘cli’) {
    9.     die(“This utility can only be run from the command line.\n);
    10. }
    11.  
    12. $counter=0;
    13. $errors=false;
    14.  
    15. function scan_dir($dir) {
    16.     $counter=0;
    17.     $dh=opendir($dir);
    18.     while ($file=readdir($dh)) {
    19.         if ($file==’.’ || $file==’..’) continue;
    20.         if (is_dir($dir.’/’.$file)) {
    21.             $counter+=scan_dir($dir.’/’.$file);
    22.         } else {
    23.                if (substr($file, strlen($file)4) == ‘.php’) {
    24.                         $counter++;
    25.                     $output=shell_exec(”/usr/bin/php –l $dir/$file 2>&1″);
    26.                     if (substr($output,0,2)!=‘No’) { // skips the “No syntax errors in …” message
    27.                         $errors=true;
    28.                         echo $output;
    29.                     }
    30.                }
    31.  
    32.         }
    33.     }
    34.     return $counter;
    35. }
    36. if ($argc!=2) {
    37.     die(“Usage: php-check dirname (usually php-check .)\n);
    38. }
    39.  
    40. if (!is_dir($argv[1])) {
    41.     die(“Argument must be a directory. The most common usage is php-check .\n);
    42. }
    43.  
    44. $counter=scan_dir($argv[1]);
    45.  
    46. echo “$counter files checked\n;
    47. exit($errors);
    48. ?>

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

    If you find it helpful, you might also want to check out scripts like PHP CodeSniffer

    or PHP Beautifier.download robot chicken star wars dvd

    No comments. You can probably see more comments on Glen's Facebook Notes page

    Leave a reply