PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > comp.lang.php > readdir() doesn't maintain order
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
readdir() doesn't maintain order

Réponse
 
LinkBack Outils de la discussion
Vieux 15/09/2007, 16h10   #1 (permalink)
Semi Confused
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut readdir() doesn't maintain order

Good Morning,

This past week I received a lot of tips on putting together:
<?php
//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 "<a href='images/$file'><img src='thumbs/$file'
class=\"pad1em\"></a>";
}
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:
<a href='images/DSC01370.jpg'><img src='thumbs/DSC01370.jpg'
class="pad1em"></a><a href='images/DSC01371.jpg'><img
src='thumbs/DSC01371.jpg' class="pad1em"></a><a
href='images/DSC01391.jpg'><img src='thumbs/DSC01391.jpg'
class="pad1em"></a><a href='images/DSC01380.jpg'><img
src='thumbs/DSC01380.jpg' class="pad1em"></a><a
href='images/DSC01382.jpg'><img src='thumbs/DSC01382.jpg'
class="pad1em"></a><a href='images/DSC01412.jpg'><img
src='thumbs/DSC01412.jpg' class="pad1em"></a><a
href='images/DSC01409.jpg'><img src='thumbs/DSC01409.jpg'
class="pad1em"></a><a href='images/DSC01397.jpg'><img
src='thumbs/DSC01397.jpg' class="pad1em"></a><a
href='images/DSC01386.jpg'><img src='thumbs/DSC01386.jpg'
class="pad1em"></a><a href='images/DSC01407.jpg'><img
src='thumbs/DSC01407.jpg' class="pad1em"></a><a
href='images/DSC01396.jpg'><img src='thumbs/DSC01396.jpg'
class="pad1em"></a><a href='images/DSC01410.jpg'><img
src='thumbs/DSC01410.jpg' class="pad1em"></a><a
href='images/DSC01359.jpg'><img src='thumbs/DSC01359.jpg'
class="pad1em"></a>

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

  Réponse avec citation
Vieux 15/09/2007, 17h03   #2 (permalink)
Andy Hassall
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: readdir() doesn't maintain order

On Sat, 15 Sep 2007 08:10:46 -0700, Semi Confused <PostInGroups@wherever.com>
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
  Réponse avec citation
Vieux 15/09/2007, 18h40   #3 (permalink)
Gordon Burditt
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: readdir() doesn't maintain order

>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.

  Réponse avec citation
Vieux 16/09/2007, 06h41   #4 (permalink)
Confused but working on it
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: readdir() doesn't maintain order

On 2007-09-15 09:03:24 -0700, Andy Hassall <andy@andyh.co.uk> said:

> On Sat, 15 Sep 2007 08:10:46 -0700, Semi Confused <PostInGroups@wherever.com>
> 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().

<?php
foreach (glob("thumbs/*.jpg") as $file)
echo "<a href='images/$file'><img src='$file' class=\"pad1em\"></a>";
?>

How do I remove the thumbs from the link on the echo?

I think you guys are messing with me.

Thanks,
Ron

  Réponse avec citation
Vieux 16/09/2007, 13h06   #5 (permalink)
Jerry Stuckle
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: readdir() doesn't maintain order

Confused but working on it wrote:
> On 2007-09-15 09:03:24 -0700, Andy Hassall <andy@andyh.co.uk> said:
>
>> On Sat, 15 Sep 2007 08:10:46 -0700, Semi Confused
>> <PostInGroups@wherever.com>
>> 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().
>
> <?php
> foreach (glob("thumbs/*.jpg") as $file)
> echo "<a href='images/$file'><img src='$file'
> class=\"pad1em\"></a>";
> ?>
>
> 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
==================
  Réponse avec citation
Vieux 16/09/2007, 15h19   #6 (permalink)
Confused but working on it
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: readdir() doesn't maintain order

On 2007-09-16 05:06:53 -0700, Jerry Stuckle <jstucklex@attglobal.net> 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

  Réponse avec citation
Vieux 16/09/2007, 18h05   #7 (permalink)
Gordon Burditt
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: readdir() doesn't maintain order

>> 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.

  Réponse avec citation
Vieux 16/09/2007, 21h59   #8 (permalink)
Confused but working on it
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: readdir() doesn't maintain order

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:
<?php
echo "Example glob<br>";
foreach (glob("thumbs/*.jpg") as $file)
echo "<a href='images/$file'><img src='$file' class=\"pad1em\"></a>";
?>

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
<?php
echo "<br>Example while readdir 1<br>";
//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 "<a href='images/$file'><img src='$file' class=\"pad1em\"></a>";
}
closedir($dir);
?>

<?php
echo "<br> Example - while readdir 2<br>";
//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 "<a href='images/$file'><img src='thumbs/$file'
class=\"pad1em\"></a>";
}
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:
<?php
echo "Example glob<br>";
foreach (glob("thumbs/*.jpg") as $file)
str_replace('thumbs/', '', $file);
echo "<a href='images/$file'><img src='$file' class=\"pad1em\"></a>";
?>
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:
<?php
echo "Example glob<br>";
foreach ((glob("thumbs/*.jpg") as $file)
str_replace('thumbs/', '', $file)
echo "<a href='images/$file'><img src='$file' class=\"pad1em\"></a>";
?>
This gives me a parse error on the foreach line. I tried a bunch of
different ways all the way to:
<?php
echo "Example glob<br>";
foreach (glob("thumbs/*.jpg") as str_replace('thumbs/', '', $file))

echo "<a href='images/$file'><img src='$file' class=\"pad1em\"></a>";
?>

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

  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 02h47.


Édité par : vBulletin® version 3.7.2
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
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,16785 seconds with 16 queries