{"id":1068,"date":"2007-10-31T08:44:30","date_gmt":"2007-10-31T16:44:30","guid":{"rendered":"http:\/\/glenandpaula.com\/wordpress\/archives\/2007\/10\/31\/scanning-a-directory-for-php-errors\/"},"modified":"2009-07-20T12:08:36","modified_gmt":"2009-07-20T20:08:36","slug":"scanning-a-directory-for-php-errors","status":"publish","type":"post","link":"https:\/\/glenandpaula.com\/wordpress\/archives\/2007\/10\/31\/scanning-a-directory-for-php-errors","title":{"rendered":"Scanning a Directory For PHP Errors"},"content":{"rendered":"<p>My fellow web geeks might find this script, <b>php-check<\/b> <em style=\"display:none\"><a href=\"http:\/\/womanhonorthyself.com\/?home_on_the_range\">download home on the range movie<\/a><\/em> , useful. It recursively scans a directory checking PHP files for syntax errors.<\/p>\n<p>Just copy it somewhere in your path (like <code>\/usr\/local\/bin<\/code>) and chmod it to&nbsp;755.&nbsp;<\/p>\n<p>I wrote the script because I edit PHP using <a href=\"http:\/\/notepad-plus.sourceforge.net\/\">Notepad++<\/a>, so it\u2019s easy for small typos to enter my scripts. I needed a quick way to scan a directory after uploading revised files.<br>\nI wrote it in PHP so that those who need it will also know how to customize it.<\/p>\n<p>[php]<br>\n#!\/usr\/bin\/php<br>\n ?php\n\/\/ php-check version 1.0\n\/\/ recursively scans a directory for .php files and runs php \u2011l on\n\/\/ them (php \u2011l checks for PHP syntax errors)\n\/\/ revisions at http:\/\/glenandpaula.com\/wordpress\/archives\/2007\/10\/31\/scanning-a-directory-for-php-errors\/\n\nif (php_sapi_name()!=\u2018cli\u2019) {\n    die(\u201cThis utility can only be run from the command line.\\n\u201d);\n}\n\n$counter=0;\n$errors=false;\n\nfunction scan_dir($dir) {\n    $counter=0;\n    $dh=opendir($dir);\n    while ($file=readdir($dh)) {\n        if ($file==\u2019.\u2019 || $file==\u2019..\u2019) continue;\n        if (is_dir($dir.\u2019\/\u2019.$file)) {\n            $counter+=scan_dir($dir.\u2019\/\u2019.$file);\n        } else {\n               if (substr($file, strlen($file) \u2014 4) == \u2018.php\u2019) {\n                        $counter++;\n                    $output=shell_exec(\u201c\/usr\/bin\/php \u2011l $dir\/$file 2&gt;&amp;1\u201d);<br>\n                    if (substr($output,0,2)!=\u2018No\u2019) { \/\/ skips the \u201cNo syntax errors in \u2026\u201d message<br>\n                        $errors=true;<br>\n                        echo $output;<br>\n                    }<br>\n               }<\/p>\n<p>        }<br>\n    }<br>\n    return $counter;<br>\n}<br>\nif ($argc!=2) {<br>\n    die(\u201cUsage: php-check dirname (usually php-check .)\\n\u201d);<br>\n}<\/p>\n<p>if (!is_dir($argv[1])) {<br>\n    die(\u201cArgument must be a directory. The most common usage is php-check .\\n\u201d);<br>\n}<\/p>\n<p>$counter=scan_dir($argv[1]);<\/p>\n<p>echo \u201c$counter files checked\\n\u201d;<br>\nexit($errors);<br>\n?&gt;<br>\n[\/php]<\/p>\n<p>This is a quick and dirty script \u2014 there are probably some bugs in it. User beware.&nbsp;<\/p>\n<p>If you find it helpful, you might also want to check out scripts like <a href=\"http:\/\/pear.php.net\/package\/PHP_CodeSniffer\">PHP CodeSniffer<\/a> <\/p>\n<ul style=\"display:none\">\n<li><a href=\"http:\/\/techbostonacademy.org\/?fiddler_on_the_roof\">fiddler on the roof&nbsp;movie<\/a><\/li>\n<\/ul>\n<div style=\"display:none\"><a href=\"http:\/\/brettmartin.org\/blog?gone\">download gone&nbsp;movie<\/a><\/div>\n<p>   or <a href=\"http:\/\/pear.php.net\/package\/PHP_Beautifier\">PHP Beautifier<\/a>.<strong style=\"display:none\"><a href=\"http:\/\/kolenalaila.com\/?robot_chicken_star_wars\">download robot chicken star wars&nbsp;dvd<\/a><\/strong><\/p>\n","protected":false},"excerpt":{"rendered":"<p>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&nbsp;755.&nbsp; I wrote the script because I edit PHP using Notepad++, so it\u2019s easy for \u2026 <a href=\"https:\/\/glenandpaula.com\/wordpress\/archives\/2007\/10\/31\/scanning-a-directory-for-php-errors\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> \u201cScanning a Directory For PHP Errors\u201d<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"wp_typography_post_enhancements_disabled":false,"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"enabled":false},"version":2}},"categories":[17],"tags":[],"class_list":["post-1068","post","type-post","status-publish","format-standard","hentry","category-geek"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/p6Ded-he","jetpack_sharing_enabled":true,"jetpack-related-posts":[],"_links":{"self":[{"href":"https:\/\/glenandpaula.com\/wordpress\/wp-json\/wp\/v2\/posts\/1068","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/glenandpaula.com\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/glenandpaula.com\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/glenandpaula.com\/wordpress\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/glenandpaula.com\/wordpress\/wp-json\/wp\/v2\/comments?post=1068"}],"version-history":[{"count":8,"href":"https:\/\/glenandpaula.com\/wordpress\/wp-json\/wp\/v2\/posts\/1068\/revisions"}],"predecessor-version":[{"id":2235,"href":"https:\/\/glenandpaula.com\/wordpress\/wp-json\/wp\/v2\/posts\/1068\/revisions\/2235"}],"wp:attachment":[{"href":"https:\/\/glenandpaula.com\/wordpress\/wp-json\/wp\/v2\/media?parent=1068"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/glenandpaula.com\/wordpress\/wp-json\/wp\/v2\/categories?post=1068"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/glenandpaula.com\/wordpress\/wp-json\/wp\/v2\/tags?post=1068"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}