473,401 Members | 2,068 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,401 software developers and data experts.

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"?

Thanks alot
Med Venlig Hilsen

Ask Josephsen
Partner og IT

web: www.MinReklame.dk
Jul 17 '05 #1
7 13841
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 / <an**@andyh.co.uk> / <http://www.andyh.co.uk>
<http://www.andyhsoftware.co.uk/space> Space: disk usage analysis tool
Jul 17 '05 #2
>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
Jul 17 '05 #3
"Gordon Burditt60" 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?
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</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-fileorde...ict142308.html
Visit Topic URL to contact author (reg. req'd). Report abuse: http://www.dbForumz.com/eform.php?p=476406
Jul 17 '05 #4
On 23 Aug 2004 17:21:26 -0400, steve <Us************@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 / <an**@andyh.co.uk> / <http://www.andyh.co.uk>
<http://www.andyhsoftware.co.uk/space> Space: disk usage analysis tool
Jul 17 '05 #5
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:41*********************@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

Jul 17 '05 #6

----- 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;
}

Jul 17 '05 #7
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:41*********************@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

Jul 17 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Oliver | last post by:
Hi, I wrote earlier this day about this problem but thought it had something to do with images. However, I found out that for some reason the readdir function is not working properly on my Suse...
2
by: Piotr Wolski | last post by:
more or less that is my problem: i'm making HTML <select><option> statement: if ($dir = @opendir("/the/directory)) { while($file = readdir($dir)) { echo '<option value="'.$file.'">'.$file;...
2
by: Matt | last post by:
Hello, I am writing a script that opens needs to get a listing of files in a directory, print that listing to a file and use that file as a quasi ftp control file. My problem is that when I...
2
by: electric sheep | last post by:
I'm not sure if this is POSIX or not ... and indeed if POSIX is OT here or not, but if the subject made sense to you .... I have a very simple program here, but it seems to be returning a "true"...
4
by: MZ | last post by:
Hello! I have written such function... Unfortunately images are not sorted by filename and I don`t know why. I have such files placed in subfolder: ...
9
by: Confused but working on it | last post by:
Just wanted to say thanks for the posts helping me make ths work. Added a few comments and after the readdir used a pattern match to test for ..jpg, and seems to work fine on my test setup. Maybe I...
1
by: mukeshrasm | last post by:
Hi, When I used readdir() function and echo the file name it prints . and .. as well.Whereas there is no such file with name . or.. , so why it is printing it. What does it mean? And one more...
2
by: kirstenkirsche | last post by:
Hi guys.... i know this question has been asked already and I found a few answers but it would be soo great if anyone could help me with this script, which just browses images on a webpage in an...
8
by: über | last post by:
Hello opendir(DIR, "."); @FILENAME = readdir(DIR); closedir(DIR); Now here u see a script that gets file names in dir the script is in, but how i could also readdir in every subdir the script...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.