For Programmers: Free Programming Magazines  


Home > Archive > PHP Programming > August 2004 > fileorder of readdir()









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author fileorder of readdir()
Ask Josephsen

2004-08-23, 3:57 pm

Hi NG

In wich order does "readdir()" read files from the disc? I've got an image
folder with images in the format "00001.jpg", "00002.jpg" etc. It seems
"readdir()" read the lowest first, but is it certain?

An alternative is to read the files into an array and sort them, does anyone
have any experience with this? Should I use the "SORT_REGULAR",
"SORT_NUMERIC" or "SORT_STRING"?

Thanks alot


Med Venlig Hilsen

Ask Josephsen
Partner og IT

web: www.MinReklame.dk


Steve

2004-08-23, 3:57 pm


See the documentation: http://www.php.net/readdir

"...The filenames are returned in the order in which they are stored by
the filesystem..."

---
Steve

Andy Hassall

2004-08-23, 8:56 pm

On Mon, 23 Aug 2004 16:01:24 +0200, "Ask Josephsen" <ask(((at)))minreklame.dk>
wrote:

>In wich order does "readdir()" read files from the disc? I've got an image
>folder with images in the format "00001.jpg", "00002.jpg" etc. It seems
>"readdir()" read the lowest first, but is it certain?


No. It's operating system and presumably filesystem dependent and totally
unreliable.

>An alternative is to read the files into an array and sort them, does anyone
>have any experience with this? Should I use the "SORT_REGULAR",
>"SORT_NUMERIC" or "SORT_STRING"?


SORT_NUMERIC doesn't make any sense unless your filenames are all numbers. The
examples you gave aren't.

If you're sorting filenames, look at natsort - it gives a much more intuitive
sort order if your filenames aren't strictly formatted and zero-padded.

--
Andy Hassall / <andy@andyh.co.uk> / <http://www.andyh.co.uk>
<http://www.andyhsoftware.co.uk/space> Space: disk usage analysis tool
Gordon Burditt

2004-08-23, 8:56 pm

>In wich order does "readdir()" read files from the disc? I've got an image
>folder with images in the format "00001.jpg", "00002.jpg" etc. It seems
>"readdir()" read the lowest first, but is it certain?


Any order it wants to. The order may seem less straightforward after
you have deleted and added files for a while. If you did something
like "cp *.jpg /usr/local/apache/data/images" it might appear you
get them in order by name, but that's not guaranteed.

>An alternative is to read the files into an array and sort them, does anyone
>have any experience with this? Should I use the "SORT_REGULAR",
>"SORT_NUMERIC" or "SORT_STRING"?


If you want a specific order, sort the files.
In what order do you want the files?

If you had file names like:
1.jpg, 2.jpg, 3.jpg, 4.jpg, 5.jpg, 6.jpg, 7.jpg, 8.jpg, 9.jpg, 10.jpg, 11.jpg ...

you might be concerned about whether 10.jpg and 11.jpg come between
1.jpg and 2.jpg, or whether they show up after 9.jpg. With the
names you have given, with a fixed number of digits, it won't
matter which sort type you use.

Gordon L. Burditt
steve

2004-08-23, 8:56 pm

"Gordon Burditt60" wrote:
> I’ve got an image
It[color=darkred]
> seems
>
> Any order it wants to. The order may seem less straightforward

after
> you have deleted and added files for a while. If you did something
> like "cp *.jpg /usr/local/apache/data/images" it might appear you
> get them in order by name, but that’s not guaranteed.
>
> does anyone
>
> If you want a specific order, sort the files.
> In what order do you want the files?
>
> If you had file names like:
> 1.jpg, 2.jpg, 3.jpg, 4.jpg, 5.jpg, 6.jpg, 7.jpg, 8.jpg, 9.jpg,

10.jpg,
> 11.jpg ...
>
> you might be concerned about whether 10.jpg and 11.jpg come between
> 1.jpg and 2.jpg, or whether they show up after 9.jpg. With the
> names you have given, with a fixed number of digits, it won’t
> matter which sort type you use.
>
> Gordon L. Burditt</font>


If you want to do something really fancy, you can always escape to
shell to do it, like:
$a = `ls -l`;
then parse $a which holds the result that came back.

--
http://www.dbForumz.com/ This article was posted by author's request
Articles individually checked for conformance to usenet standards
Topic URL: http://www.dbForumz.com/PHP-fileord...pict142308.html
Visit Topic URL to contact author (reg. req'd). Report abuse: http://www.dbForumz.com/eform.php?p=476406
Andy Hassall

2004-08-23, 8:56 pm

On 23 Aug 2004 17:21:26 -0400, steve <UseLinkToEmail@dbForumz.com> wrote:

>If you want to do something really fancy, you can always escape to
>shell to do it, like:
>$a = `ls -l`;
>then parse $a which holds the result that came back.


Ugh - you then have the following problems:

(1) Worries about shell injection attacks if you pass any parameters.
(2) Are you sure 'ls' on your PATH does what you want?
(3) Extra overhead of spawning off new processes per reqest.
(4) Loss of portability - ls doesn't exist on Windows.
(5) Is the output of ls even standardised? Across all versions of Unix?
(6) Does it sort in the way wanted by the OP?

You're almost certainly better off sticking to readdir and sort it afterwards.

--
Andy Hassall / <andy@andyh.co.uk> / <http://www.andyh.co.uk>
<http://www.andyhsoftware.co.uk/space> Space: disk usage analysis tool
Ask Josephsen

2004-08-24, 3:57 am

Thanks alot - your comments are very helpfull :)

--

Med Venlig Hilsen

Ask Josephsen
Partner og IT

web: www.MinReklame.dk


"Ask Josephsen" <ask(((at)))minreklame.dk> wrote in message
news:4129f8b6$0$213$14726298@news.sunsite.dk...
> Hi NG
>
> In wich order does "readdir()" read files from the disc? I've got an image
> folder with images in the format "00001.jpg", "00002.jpg" etc. It seems
> "readdir()" read the lowest first, but is it certain?
>
> An alternative is to read the files into an array and sort them, does

anyone
> have any experience with this? Should I use the "SORT_REGULAR",
> "SORT_NUMERIC" or "SORT_STRING"?
>
> Thanks alot
>
>
> Med Venlig Hilsen
>
> Ask Josephsen
> Partner og IT
>
> web: www.MinReklame.dk
>
>



Varavva Yevgen aka xa4anypu

2004-08-29, 3:56 pm


----- Original Message -----
From: "Ask Josephsen" <ask(((at)))minreklame.dk>
Newsgroups: comp.lang.php
Sent: Monday, August 23, 2004 5:01 PM
Subject: fileorder of readdir()


> Hi NG
>
> In wich order does "readdir()" read files from the disc? I've got an image
> folder with images in the format "00001.jpg", "00002.jpg" etc. It seems
> "readdir()" read the lowest first, but is it certain?
>
> An alternative is to read the files into an array and sort them, does

anyone
> have any experience with this? Should I use the "SORT_REGULAR",
> "SORT_NUMERIC" or "SORT_STRING"?


Try to put all folder content with readdir() to an array, than usort() with
this function:
function cmp ($el_1,$el_2)
{
if(is_dir($el_1) && !is_dir($el_2)) return -1;
if(!is_dir($el_1) && is_dir($el_2)) return 1;
if($el_1<$el_2) return -1;
elseif($el_1>$el_2) return 1;
else return 0;
}





Ask Josephsen

2004-08-30, 8:57 am

Thanks for all your tips :)

definately helped me out

--

Med Venlig Hilsen

Ask Josephsen
Partner og IT

web: www.MinReklame.dk


"Ask Josephsen" <ask(((at)))minreklame.dk> wrote in message
news:4129f8b6$0$213$14726298@news.sunsite.dk...
> Hi NG
>
> In wich order does "readdir()" read files from the disc? I've got an image
> folder with images in the format "00001.jpg", "00002.jpg" etc. It seems
> "readdir()" read the lowest first, but is it certain?
>
> An alternative is to read the files into an array and sort them, does

anyone
> have any experience with this? Should I use the "SORT_REGULAR",
> "SORT_NUMERIC" or "SORT_STRING"?
>
> Thanks alot
>
>
> Med Venlig Hilsen
>
> Ask Josephsen
> Partner og IT
>
> web: www.MinReklame.dk
>
>



Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2010 codecomments.com