Link to home
Start Free TrialLog in
Avatar of Vlearns
Vlearns

asked on

PHP directory navigation

Hi

Can some one help me achieve the following in php

this is what to do.

1) give my php program a directory where there are a bunch of php files.
2) taking each of these files at a time, i want to run php -l against it so that i can catch if there are any syntax errors in the file.
3)I want to print out the names of the files for which php -l failed and maybe send an email


pseudo code

1. go to dir

2. for all files in dir do

     php -l  filename.php

3. if php -l failed log the name of the file in a report or send an email right away.


Avatar of Richard Quadling
Richard Quadling
Flag of United Kingdom of Great Britain and Northern Ireland image

The attached script will report which php scripts have errors.

You can run it with or without a path as the first and only parameter.

It requires PHP5 (for the SPL DirectoryIterator).

If no path is supplied, the current path is used.


<?php
$s_Location = isset($argv[1]) && strlen($argv[1]) > 0 ? $argv[1] : '.';
foreach(new DirectoryIterator($s_Location) as $o_Entry) {
	if ('.php' == strtolower(substr($o_Entry, -4))) {
		if ('No syntax errors detected in ' . $o_Entry !== ($s_Error = exec('\php\php.exe -l ' . $o_Entry))) {
			echo $s_Error, PHP_EOL;
		}
	}
}

Open in new window

To make it send an email ... (untested).
<?php
$s_Location = isset($argv[1]) && strlen($argv[1]) > 0 ? $argv[1] : '.';
$s_Errors = '';
foreach(new DirectoryIterator($s_Location) as $o_Entry) {
	if ('.php' == strtolower(substr($o_Entry, -4))) {
		if ('No syntax errors detected in ' . $o_Entry !== ($s_Error = exec('\php\php.exe -l ' . $o_Entry))) {
			$s_Errors .= $s_Error, PHP_EOL;
		}
	}
}
 
mail('me@work.com', 'PHP lint failures', $s_Errors);

Open in new window

Ha! I said untested!
<?php
$s_Location = isset($argv[1]) && strlen($argv[1]) > 0 ? $argv[1] : '.';
$s_Errors = '';
foreach(new DirectoryIterator($s_Location) as $o_Entry) {
	if ('.php' == strtolower(substr($o_Entry, -4))) {
		if ('No syntax errors detected in ' . $o_Entry !== ($s_Error = exec('\php\php.exe -l ' . $o_Entry))) {
			$s_Errors .= $s_Error . PHP_EOL;
		}
	}
}
 
mail('me@work.com', 'PHP lint failures', $s_Errors);

Open in new window

Avatar of Vlearns
Vlearns

ASKER

Hi

thanks for your post,

still trying to figure it out, i introduced some php files with errors and it sent me just an email

with subject "php lint errors, is there a way i can get the name of the files for which it failed?

ASKER CERTIFIED SOLUTION
Avatar of Richard Quadling
Richard Quadling
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Vlearns

ASKER

thanks a lot, that really helped!!!!
Avatar of Vlearns

ASKER

thanks a million!!!