If we don’t use exec(“ls -l”) in PHP, but use readdir() or scandir() to get the list of files, and want to list the file sizes and maybe modification date as well, do we need to call stat() 1000 times if there are 1000 files? Is there a simpler or faster way?
Will calling stat() 1000 times hit the file system a lot? Or maybe the OS will cache all the data in memory so it involves only access to RAM but not to the disk.
Update: it looks good to use DirectoryIterator(), but how do you sort the items?
LS calls the stat function in the background for you. Listing a directory will only give you the names of the files/folders in question, so you must manually stat each filename returned to get information about each file itself. So yes, you must call stat 1000 times for 1000 files to get all their info. Ideally you will do it in loop.