look at all files, then go elsewhere

This is a discussion on look at all files, then go elsewhere within the PHP General forums, part of the PHP Programming Forums category; All, I'm trying to figure out the logic piece, and I'm pretty sure I am missing something simple. ...


Go Back   Usenet Forums > PHP Programming Forums > PHP General

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 09-11-2007
Wolf
 
Posts: n/a
Default look at all files, then go elsewhere

All,

I'm trying to figure out the logic piece, and I'm pretty sure I am missing something simple.

I have a script that I want to check all files in a directory for information, if it doesn't find it when all done, I want it to go elsewhere, however if it does find it, I want it to break out of the search and perform a function.

CODE:
if ($userinfo == "")
{
if ($handle = opendir('./bp_csv/')) {
while (false !== ($file = readdir($handle))) {
if($file != '..' && $file != '.') {
if(is_dir($file)) { //Diretory skipped
} else {
$command = 'cat ./bp_csv/' . $file . ' | grep '"';
$user = $_POST[userid];
$command .= $user . '"'';
$userinfo=exec($command);
// here's where it is multi-looping
if ($userinfo =="")
{
echo "$user not found in any BP_CSV files, now running LDAP check<BR>";
ldap_check($user);
}
else
{
// ok, found the person, run the function
userprofile($userinfo);
}
}
}
}
}
}
else
{
//original data has contents, go after user data
userprofile($userinfo);
}

--------------- END CODE

Wolf
Reply With Quote
  #2 (permalink)  
Old 09-11-2007
Jay Blanchard
 
Posts: n/a
Default RE: [PHP] look at all files, then go elsewhere

[snip]
I have a script that I want to check all files in a directory for
information, if it doesn't find it when all done, I want it to go
elsewhere, however if it does find it, I want it to break out of the
search and perform a function.

CODE:
if ($userinfo == "")
{
if ($handle = opendir('./bp_csv/')) {
while (false !== ($file = readdir($handle))) {
if($file != '..' && $file != '.') {
if(is_dir($file)) { //Diretory skipped
} else {
$command = 'cat ./bp_csv/' . $file . ' | grep '"';
$user = $_POST[userid];
$command .= $user . '"'';
$userinfo=exec($command);
// here's where it is multi-looping
if ($userinfo =="")
{
echo "$user not found in any BP_CSV files, now running LDAP
check<BR>";
ldap_check($user);
}
else
{
// ok, found the person, run the function
userprofile($userinfo);
}
}
}
}
}
}
else
{
//original data has contents, go after user data
userprofile($userinfo);
}
[/snip]

Put all of the locations you want to search in an array and loop through
the array.
Reply With Quote
  #3 (permalink)  
Old 09-11-2007
brian
 
Posts: n/a
Default Re: [PHP] look at all files, then go elsewhere

Wolf wrote:
> All,
>
> I'm trying to figure out the logic piece, and I'm pretty sure I am
> missing something simple.
>
> I have a script that I want to check all files in a directory for
> information, if it doesn't find it when all done, I want it to go
> elsewhere, however if it does find it, I want it to break out of the
> search and perform a function.
>
> CODE:
> if ($userinfo == "")
> {
> if ($handle = opendir('./bp_csv/')) {
> while (false !== ($file = readdir($handle))) {
> if($file != '..' && $file != '.') {
> if(is_dir($file)) { //Diretory skipped
> } else {
> $command = 'cat ./bp_csv/' . $file . ' | grep '"';
> $user = $_POST[userid];
> $command .= $user . '"'';
> $userinfo=exec($command);


You'd sanitise this in real life, i hope. escapeshellarg() and
escapeshellcmd() are your good friends.

curl -d "user=foo%22%3Becho+%22pwned%21" http://yourdomain/yourscript.php

And i think you have an extra single quote there at the end of both
those lines.

Why aren't you simply reading into an array the contents of the file and
searching for $user in that? Use file()

http://www.php.net/manual/en/function.file.php

-- snip --
while (false !== ($file = readdir($handle)))
{
$lines = file($file);
$idx = array_search($user, $lines)

if ($idx !== FALSE)
{
$userinfo = $line;
break;
}
}

if ($userinfo != "")
{
userprofile($userinfo);
}
else
{
...
-- snip --

Or something like that.

> // here's where it is multi-looping
> if ($userinfo =="")
> {
> echo "$user not found in any BP_CSV files, now running LDAP check<BR>";
> ldap_check($user);
> }
> else
> {
> // ok, found the person, run the function
> userprofile($userinfo);
> }
> }
> }
> }
> }
> }
> else
> {
> //original data has contents, go after user data
> userprofile($userinfo);
> }
>
> --------------- END CODE


The reason your script was looping is because your second "if ($userinfo
=="")" is reached for each and every file found in the directory. That
is, every time you grep for $user in a file, you're re-setting
$userinfo, regardless of the result. Then you're running ldap_check().

Basically, you need a break in there, at the very minimum. And your
second test on $userinfo should be outside of the while loop.

Regardless, use file() instead of that exec() call. That looks as if it
could have sharp edges.

brian
Reply With Quote
  #4 (permalink)  
Old 09-11-2007
Jim Lucas
 
Posts: n/a
Default Re: [PHP] look at all files, then go elsewhere

Wolf wrote:
> All,
>
> I'm trying to figure out the logic piece, and I'm pretty sure I am missing something simple.
>
> I have a script that I want to check all files in a directory for information, if it doesn't find it when all done, I want it to go elsewhere, however if it does find it, I want it to break out of the search and perform a function.
>
> Wolf
>


based off your logic, I think I fixed your problem.

don't forget about http://us3.php.net/break

// Get a directory resource handler
if ($handle = opendir('./bp_csv/')) {

// Looping...
while ( $file = readdir($handle) ) {

// This checks to see if it is a file. Not a symlink, dir, socket, etc...
// Your is_dir() test would have failed if it had come across a socket
if ( is_file($file) ) {

// Mind you that I have not used the escapeshell***() commands
// that much. You might need to drop the escapeshellcmd(), since
// you are creating the command inline, and you have already escaped
// the input data with escapeshellarg()

// Be sure to validate input
$user = escapeshellarg(@$_POST[userid]);

// Be sure to escape the command also
$command = escapeshellcmd("grep \"{$user}\" ./bp_csv/{$file}");

// Run command and capture results
$userinfo = exec($command);

// Check to see if I got any results
// Might want to do a little better result checking.
// This would pass even if the command failes and hands back an error
if ( ! empty($userinfo) ) {

// ok, found the person, run the function
userprofile($userinfo);

// Exit while loop
break;
}
}
}
}
// Nothing was returned for any of the files that we found.
if ( empty($userinfo) ) {
echo "$user not found in any BP_CSV files, now running LDAP check<BR>";
ldap_check($user);
}



--
Jim Lucas

"Some men are born to greatness, some achieve greatness,
and some have greatness thrust upon them."

Twelfth Night, Act II, Scene V
by William Shakespeare
Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT +1. The time now is 04:50 AM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0