readdir() doesn"t maintain order
readdir() doesn"t maintain order
am 15.09.2007 17:10:46 von Confused but working on it
Good Morning,
This past week I received a lot of tips on putting together:
//Open images directory
$dir = opendir("thumbs");
//read files in the dir
while (($file = readdir($dir)) !== false)
//test to make sure jpg
if (eregi("\.jpg",$file))
{
echo "
class=\"pad1em\">";
}
closedir($dir);
?>
Images and thumbs are generated for the same album in iPhoto so
filenames are consistent. One export for thumbs a second for images.
Was reviewing a few of the pages I made and something seemed odd.
Further investigation showed that my thumbs are not in the order of the
filesystem. Here is the source from the output:
class="pad1em">
src='thumbs/DSC01371.jpg' class="pad1em">
href='images/DSC01391.jpg'>
class="pad1em">
src='thumbs/DSC01380.jpg' class="pad1em">
href='images/DSC01382.jpg'>
class="pad1em">
src='thumbs/DSC01412.jpg' class="pad1em">
href='images/DSC01409.jpg'>
class="pad1em">
src='thumbs/DSC01397.jpg' class="pad1em">
href='images/DSC01386.jpg'>
class="pad1em">
src='thumbs/DSC01407.jpg' class="pad1em">
href='images/DSC01396.jpg'>
class="pad1em">
src='thumbs/DSC01410.jpg' class="pad1em">
href='images/DSC01359.jpg'>
class="pad1em">
So it starts at pic 1370 and ends back at 1359. In reality the pics
start at 1359 and end at 1424. Maybe the question is how is the file
system really sorted? Using Filezilla it appears to be ascending by
filename. Date and time of shots are ascending as this is all from the
same "roll".
I haven't tried to implement the other methods shown this week and
maybe one doesn't do this?
or
Readdir into an array?
It's too early for this. :)
Thanks,
Ron
Re: readdir() doesn"t maintain order
am 15.09.2007 18:03:24 von Andy Hassall
On Sat, 15 Sep 2007 08:10:46 -0700, Semi Confused
wrote:
>$dir = opendir("thumbs");
>//read files in the dir
>while (($file = readdir($dir)) !== false)
>
>Was reviewing a few of the pages I made and something seemed odd.
>Further investigation showed that my thumbs are not in the order of the
>filesystem.
>
>So it starts at pic 1370 and ends back at 1359. In reality the pics
>start at 1359 and end at 1424. Maybe the question is how is the file
>system really sorted?
Yes.
>I haven't tried to implement the other methods shown this week and
>maybe one doesn't do this?
>or
>Readdir into an array?
Yes, or scandir() (which gives a sorted array), or glob() (which both filters
and sorts), but in either case you may still want a different sort order - in
particular look at natsort().
http://php.net/scandir
http://php.net/glob
http://php.net/natsort
--
Andy Hassall :: andy@andyh.co.uk :: http://www.andyh.co.uk
http://www.andyhsoftware.co.uk/space :: disk and FTP usage analysis tool
Re: readdir() doesn"t maintain order
am 15.09.2007 19:40:24 von gordonb.onktf
>Was reviewing a few of the pages I made and something seemed odd.
>Further investigation showed that my thumbs are not in the order of the
>filesystem.
I believe that the order in which readdir() returns entries is *BY
DEFINITION* the order of the filesystem. If you want alphabetical
order or something else, sort it yourself.
>So it starts at pic 1370 and ends back at 1359. In reality the pics
>start at 1359 and end at 1424. Maybe the question is how is the file
>system really sorted?
It isn't.
>Using Filezilla it appears to be ascending by
>filename. Date and time of shots are ascending as this is all from the
>same "roll".
Many programs, like "ls" sort the file names for display purposes.
If you want a particular order (e.g. by name or file time or size),
sort it yourself. "ls -f" gives you unsorted output if your ls has
that option.
Re: readdir() doesn"t maintain order
am 16.09.2007 07:41:17 von Confused but working on it
On 2007-09-15 09:03:24 -0700, Andy Hassall said:
> On Sat, 15 Sep 2007 08:10:46 -0700, Semi Confused
> wrote:
>
>> $dir = opendir("thumbs");
>> //read files in the dir
>> while (($file = readdir($dir)) !== false)
>>
>> Was reviewing a few of the pages I made and something seemed odd.
>> Further investigation showed that my thumbs are not in the order of the
>> filesystem.
>> So it starts at pic 1370 and ends back at 1359. In reality the pics
>> start at 1359 and end at 1424. Maybe the question is how is the file
>> system really sorted?
>
> Yes.
>
>> I haven't tried to implement the other methods shown this week and
>> maybe one doesn't do this?
>> or
>> Readdir into an array?
>
> Yes, or scandir() (which gives a sorted array), or glob() (which both filters
> and sorts), but in either case you may still want a different sort order - in
> particular look at natsort().
>
> http://php.net/scandir
> http://php.net/glob
> http://php.net/natsort
So I kludged this together and it doesn't fail but doesn't display my
image after I click the thumb. glob() says it searches for all
pathnames so if the file was where the code is then the filname is the
pathname. I think. when i mouseover I can see that the path for the
image has "thumbs" in it as it is part of the path from blob().
foreach (glob("thumbs/*.jpg") as $file)
echo "";
?>
How do I remove the thumbs from the link on the echo?
I think you guys are messing with me. :)
Thanks,
Ron
Re: readdir() doesn"t maintain order
am 16.09.2007 14:06:53 von Jerry Stuckle
Confused but working on it wrote:
> On 2007-09-15 09:03:24 -0700, Andy Hassall said:
>
>> On Sat, 15 Sep 2007 08:10:46 -0700, Semi Confused
>>
>> wrote:
>>
>>> $dir = opendir("thumbs");
>>> //read files in the dir
>>> while (($file = readdir($dir)) !== false)
>>>
>>> Was reviewing a few of the pages I made and something seemed odd.
>>> Further investigation showed that my thumbs are not in the order of
>>> the filesystem.
>>> So it starts at pic 1370 and ends back at 1359. In reality the pics
>>> start at 1359 and end at 1424. Maybe the question is how is the file
>>> system really sorted?
>>
>> Yes.
>>
>>> I haven't tried to implement the other methods shown this week and
>>> maybe one doesn't do this?
>>> or
>>> Readdir into an array?
>>
>> Yes, or scandir() (which gives a sorted array), or glob() (which both
>> filters
>> and sorts), but in either case you may still want a different sort
>> order - in
>> particular look at natsort().
>>
>> http://php.net/scandir
>> http://php.net/glob
>> http://php.net/natsort
>
> So I kludged this together and it doesn't fail but doesn't display my
> image after I click the thumb. glob() says it searches for all pathnames
> so if the file was where the code is then the filname is the pathname. I
> think. when i mouseover I can see that the path for the image has
> "thumbs" in it as it is part of the path from blob().
>
>
> foreach (glob("thumbs/*.jpg") as $file)
> echo "
> class=\"pad1em\">";
> ?>
>
> How do I remove the thumbs from the link on the echo?
>
Maybe something like
str_replace('thumbs/', '', $file);
> I think you guys are messing with me. :)
>
And why would we do that?
> Thanks,
> Ron
>
--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================
Re: readdir() doesn"t maintain order
am 16.09.2007 16:19:05 von Confused but working on it
On 2007-09-16 05:06:53 -0700, Jerry Stuckle said:
> str_replace('thumbs/', '', $file);
I put this right above my echo and did get an image. But only one. It
was the last file in the directory. Also tried to replace $file within
my output line but failed.
Maybe my coffee hasn't kicked in.
Thx..ron
Re: readdir() doesn"t maintain order
am 16.09.2007 19:05:28 von gordonb.uwl0i
>> str_replace('thumbs/', '', $file);
>
>I put this right above my echo and did get an image. But only one. It
>was the last file in the directory. Also tried to replace $file within
>my output line but failed.
>Maybe my coffee hasn't kicked in.
After you added the str_replace() call, is the echo INSIDE or OUTSIDE
of the foreach() loop?
What do you need to have after foreach() to ensure that the following
TWO statements are inside the loop?
Although you can get advice here, don't expect it to be click-by-click,
keystroke-by-keystroke instructions.
Re: readdir() doesn"t maintain order
am 16.09.2007 22:59:35 von Confused but working on it
On 2007-09-16 10:05:28 -0700, gordonb.uwl0i@burditt.org (Gordon Burditt) said:
>>> str_replace('thumbs/', '', $file);
>>
>> I put this right above my echo and did get an image. But only one. It
>> was the last file in the directory. Also tried to replace $file within
>> my output line but failed.
>> Maybe my coffee hasn't kicked in.
>
> After you added the str_replace() call, is the echo INSIDE or OUTSIDE
> of the foreach() loop?
>
> What do you need to have after foreach() to ensure that the following
> TWO statements are inside the loop?
>
> Although you can get advice here, don't expect it to be click-by-click,
> keystroke-by-keystroke instructions.
Now that's a good lead. In my attempts using readdir it would read then
echo, read then echo. I'm learning a bit as I go and having a bit of
fun. Here are a few things that work:
echo "Example glob
";
foreach (glob("thumbs/*.jpg") as $file)
echo "";
?>
This works meaning it doesn't fail until I click the link. Will get
back to a couple things I've tried.
Here are two more samples. They look exactly the same but in the same
page example 1 gives me the image marker for the thumb and the link to
the larger picture works. Example 2 shows the thumb and the link works.
I see no difference in the code. These do what I want
echo "
Example while readdir 1
";
//Open images directory
$dir = opendir("thumbs");
//read files in the dir
while(($file = readdir($dir)) !== false)
//test to make sure jpg
if(eregi("\.jpg",$file))
{
echo "";
}
closedir($dir);
?>
echo "
Example - while readdir 2
";
//Open images directory
$dir = opendir("thumbs");
//read files in the dir
while (($file = readdir($dir)) !== false)
//test to make sure jpg
if (eregi("\.jpg",$file))
{
echo "
class=\"pad1em\">";
}
closedir($dir);
?>
These do what I want which is a thumb linking to a larger picture.This
doesn't leave the intended result of file sort order when I upload.
Still accomplishes 90% of what I'm looking for.
Back to making the glob example work. You say is that bit of code
inside or outside the foreach loop. Doesn't it "foreach" until it'
done? By the example above it does. So when I said I put my line in
between the foreach line and my echo line that's what I meant. This
gives me one thumb that still has the images in the link:
echo "Example glob
";
foreach (glob("thumbs/*.jpg") as $file)
str_replace('thumbs/', '', $file);
echo "";
?>
Removing the semi in the str_replace line gives me an error on the echo
line. So if you really mean insie I did this:
echo "Example glob
";
foreach ((glob("thumbs/*.jpg") as $file)
str_replace('thumbs/', '', $file);)
echo "";
?>
This gives me a parse error on the foreach line. I tried a bunch of
different ways all the way to:
echo "Example glob
";
foreach (glob("thumbs/*.jpg") as str_replace('thumbs/', '', $file))
echo "";
?>
This error is a bit different - Parse error: parse error, expecting
`T_VARIABLE' or `'$'' i
This is all I've tried so far. Will post back if I get it.
thx..ron