Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

my first php script - pls help!!!

Status
Not open for further replies.

santanudas

Technical User
Mar 18, 2002
121
GB
Hi people,

I'm a complete newbie in this php world and was trying a very simple thing to start with. But can't see that I'm doing quite well. These are the things I'm trying to do:

list the directory content using UNIX “ls” command
split the out put
put the values in an array
display the result.

And I came up with something like this:
Code:
<?php
$this_dir = "images";
$dir_path = "/project/web/$this_dir";
$LS = "/bin/ls";

$CMD=$LS . " " . $dir_path . "/thumbs";

$thumbs = exec($CMD);
$qp = preg_split('/\n/', $thumbs);
$qq = array($qp);

$COL = count($qq);
echo ("Number of elements in the array: $COL <br><br>\n");

for($qx = 1; $qx<=$COL; ++$qx) {
   $px = $qx-1;
   echo ( "$qx) $thumbs[$px] <br>\n" );
}
?>
But it's not working. I think the splitting and putting into an array parts are not working. Any help from any one is very much appreciated.

 
I think the splitting and putting
You think? Or have you verifed?

Verify that the values are getting into $thumbs to start with by footprinting your code. Change the line

[tt]$thumbs = exec($CMD);[/tt]

to read

[tt]$thumbs = exec($CMD);
print $thumbs;die();[/tt]

and see if the value is there.


I have no idea what you indend this line:

[tt]$qq = array($qp);[/tt]

to do. preg_split(), as is explained in its online manual page outputs an array. What more do you need to do with it? Change these lines:

[tt]$qp = preg_split('/\n/', $thumbs);
$qq = array($qp)[/tt]

to read:

[tt]$qq = preg_split('/\n/', $thumbs);[/tt]






Want the best answers? Ask the best questions! TANSTAAFL!
 
Thanks for your reply, sleipnir214.
You think? Or have you verifed?
I have “kinda” verified but I said: “I think”, because I wasn't very sure. As I know almost nothing about php, so I thought you people probably find out other mistakes, which actually causing the errors.
$thumbs = exec($CMD);
print $thumbs;die();
I didn't actually try with “print” but I did try with echo, like this: echo ( "$thumbs <br>\n" );
but all it did, just print the last line of the output. The same result I see using the print as well.

In stead of using exec(), I tried the same thing with system() and then I could see the whole output.
I have no idea what you indend this line:
$qq = array($qp);
I know that was stupid, but I was just trying several thing. I already tried $qq = preg_split('/\n/', $thumbs); but didn't bring any luck, so far.
 
Let's talk about some other things...

Like why in this line:

echo ( "$qx) $thumbs[$px] <br>\n" );

you are treating $thumbs like an array when it is not. The preg_split() line sets another variable to an array.


Also, you must read the PHP online manual entry on Types to understand the different between [tt]'\n'[/tt] and [tt]"\n"[/tt]. In PHP, the singlequote and the doublequote are not interchangeable.

I also want to draw your attention to PHP's glob()[/function]. Using that, your code simplifies to something like:

Code:
<?php
$this_dir = 'images';
$dir_path = '/project/web/' . $this_dir;

$qq = glob($dir_path . '/thumbs' . '/*');

$COL = count($qq);
echo ("Number of elements in the array: $COL <br><br>\n");

for($qx = 0; $qx < $COL; ++$qx)
{
   $px = $qx + 1;
   echo ( "$px) $qq[$qx] <br>\n" );
}
?>



Want the best answers? [url=http://www.catb.org/~esr/faqs/smart-questions.html]Ask the best questions!
TANSTAAFL!
 
Like why in this line:
echo ( "$qx) $thumbs[$px] <br>\n" );

Yeah, you are right, that was my mistake. But when I was using $qq but I pasted the wrong version here.

In the end, I think there are some thing wrong with preg_split() function, that doesn't work properly. Using explode() instead, did the job. So, now I end up with this, which is working:
Code:
$this_dir = "images";
$dir_path = "/project/web/$this_dir";
$LS = "/bin/ls";

$cmd=$LS . " " . $dir_path . "/thumbs";
$qq = explode("\n",`$cmd`);

$COL = count($qq);
echo ("Number of elements: $COL <br><br>\n");

for($qx = 1; $qx<$COL; $qx++) {
   $px = $qx-1;
   echo ( "$qx) $qq[$px] <br>\n" );
}
except one small thing. $COL = count($qq); is returning a number, which is one grated than the actual number it should be. Thus, I get the right number, if i do like this:
Code:
$COL = count($qq);
$COL = $COL-1;
Any idea why? Do I need to use trim() somewhere?

 
Do you have blank array elements?
I guess so; explode() probably adding a blank element to the array in the end (???). In this case, the number of array elements should be the equivalent to the number of files in that directory. If I physically go to that directory and issue ls | wc -l, I get the correct number. But the array is one higher than the actual number. Am i missing something?



 
explode() does not add elements arbitrarily. if you have a line space at the end of your text then this might be the culprit.

try
Code:
exec($cmd, $qq);
the output should be in $qq as an array and this method should trailing line breaks.
 
Thanks jpadie , that was perfect. Now things are started happening as I was expecting. I'm still a bit confused though. I'm pretty sure that the line space at the end is the culprit but any idea how that blank space is being created? Is it because of using “\n” in explode()?

 
no ... or yes. the \n is just a line break. the exec command trims line breaks at the end. or ignores them. so you don't end up with an extra element.

i suspect that the ls command applies a line break at the end of every line, including the last, in order to take the cursor back to the hard left edge. it was, therefore, mainly the action of ls that was the culprit.

but, as sleipnir214 points out, there are much better ways to grab directory listings. readdir() and glob() for example.

you might also want to play with the options to ls in order to suppress the dot and doubledot
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top