Jump to content

in what order files are returned with readdir()


sunilvadranapu

Recommended Posts

Hi all, i read in php.net that readdir() returns next filename from the directory and filenames are returned in the order in which they are stored by the filesystem.

 

how to know in which order files are stored in the file system.

 

To know the order(whether time or alphabetic) of the files, i created below files(with date of creation) on my linux system

# ls -ltr
rw-r--r-- 1 root root   12861   2009-11-11 16:20 sunil.txt
-rw-r--r-- 1 root root  2157    2009-11-11 16:21 raja.txt
-rw-r--r-- 1 root root  1334    2009-11-11 16:22 ben.txt
-rw-r--r-- 1 root root     6      2009-11-11 16:23 basha.txt
-rw-r--r-- 1 root root     6      2009-11-11 16:24 abi.txt

 

Next i wrote a small PHP script to know which order does readdir() function reads these files:

 

if ($handle = opendir($localpath)) {
   while (($filename = readdir($handle)) !== false) {
     echo "LocalFileName:: ".$filename."\n";
     if (is_file("$localpath/$filename")) {
        $fsize = filesize("$localpath/$filename");
         echo "FileSize:: ".$fsize."\n";
     } //if
    } //while

Result is -
LocalFileName:: basha.txt
FileSize:: 6
LocalFileName:: sunil.txt
FileSize:: 12861
LocalFileName:: raja.txt
FileSize:: 2157
LocalFileName:: abi.txt
FileSize:: 6

 

i am totally confused to find the order in which files are returned by readdir();

 

i thought it would return the files order by timestamp(date of creation) or by alphabetic order.  but to my surprise it returned in different order.

 

please any one help me by explaining what order readdir follows to return files in dir. does it return in random order? i dont think it follows random order, because i run the above script for many times, all the time it returned same order.

 

could some one please clarify my doubts.

 

thanks in advance.

Link to comment
Share on other sites

the order in which they are stored by the filesystem.

 

Simplified version -

 

A file system is like a database. As files are added or removed, the file information gets added or removed in available spaces in the directory table. If you remove a couple of files, then add more, the empty spaces in the directory table will be used first, then more entires will be added to the end. Some disk optimization utilities actually reorder the entries in the directory table so that they are alphabetical.

 

The order that readdir() returns names in is the order of the file names in the directory table. The only way to guarantee that your code operates on a list that is in the order that you want it to be is if you read the list into an array and sort the array the way you want.

 

Edit: Or use one of the functions like glob() that can return the list in alphabetical order.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.