PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > php.general > look at all files, then go elsewhere
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
look at all files, then go elsewhere

Réponse
 
LinkBack Outils de la discussion
Vieux 11/09/2007, 15h36   #1
Wolf
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut 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
  Réponse avec citation
Vieux 11/09/2007, 16h23   #2
Jay Blanchard
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut 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.
  Réponse avec citation
Vieux 11/09/2007, 16h38   #3
brian
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut 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
  Réponse avec citation
Vieux 11/09/2007, 16h41   #4
Jim Lucas
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut 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
  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 16h13.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,12724 seconds with 12 queries