• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 827
  • Last Modified:

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.


0
Vlearns
Asked:
Vlearns
  • 4
  • 3
1 Solution
 
Richard QuadlingSenior Software DeveloperCommented:
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

0
 
Richard QuadlingSenior Software DeveloperCommented:
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

0
 
Richard QuadlingSenior Software DeveloperCommented:
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

0
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
VlearnsAuthor Commented:
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?

0
 
Richard QuadlingSenior Software DeveloperCommented:
That SHOULD have been in the body of the message.

Try this snippet. This prints the errors as well as sends an email.


<?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;
		}
	}
}
if ('' !== $s_Errors) {
	echo 'PHP lint failures.', PHP_EOL, $s_Errors;
	mail('me@work.com', 'PHP lint failures', $s_Errors);
}

Open in new window

0
 
VlearnsAuthor Commented:
thanks a lot, that really helped!!!!
0
 
VlearnsAuthor Commented:
thanks a million!!!
0

Featured Post

Receive 1:1 tech help

Solve your biggest tech problems alongside global tech experts with 1:1 help.

  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now