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.


VlearnsAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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
Cloud Class® Course: Microsoft Windows 7 Basic

This introductory course to Windows 7 environment will teach you about working with the Windows operating system. You will learn about basic functions including start menu; the desktop; managing files, folders, and libraries.

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
VlearnsAuthor Commented:
thanks a lot, that really helped!!!!
0
VlearnsAuthor Commented:
thanks a million!!!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.