sunilvadranapu Posted November 12, 2009 Share Posted November 12, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/181227-in-what-order-files-are-returned-with-readdir/ Share on other sites More sharing options...
PFMaBiSmAd Posted November 12, 2009 Share Posted November 12, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/181227-in-what-order-files-are-returned-with-readdir/#findComment-956047 Share on other sites More sharing options...
sunilvadranapu Posted November 13, 2009 Author Share Posted November 13, 2009 PFMaBiSmAd, thanks for your reply and i think i can explore with the inputs that you gave. thanks alot. Quote Link to comment https://forums.phpfreaks.com/topic/181227-in-what-order-files-are-returned-with-readdir/#findComment-956676 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.