phpknight Posted August 6, 2007 Share Posted August 6, 2007 Are there php functions that get the number of subdirectories or files in a given directory? I cannot locate one. If so, is it fast? I plan on having a lot of files/directories, so I just want a number, not a list. The server people tell me that only approx. 32000 subdirectories can be in any given directory, so I want to switch directories when it starts getting near that limit. This is thinking way ahead, but I would rather plan for it now than encounter problems later. Quote Link to comment https://forums.phpfreaks.com/topic/63625-solved-functions-for-num-subdirectories-and-files-in-a-directory/ Share on other sites More sharing options...
Fadion Posted August 6, 2007 Share Posted August 6, 2007 Basically files and folder in a directory could be counted like: $path = 'somedir'; $dir = opendir($dir); $counter = 0; while (($file = readdir($dir)) !== false) { $counter++; } echo $counter; U will need to make a recursive function which if finds a directory with is_dir(), starts itself over. Im currently thinking of creating a file manager and maybe ill try creating such an option which counts the total files and subdirectories of a directory. If ill have smth before you get this done, ill write it. Quote Link to comment https://forums.phpfreaks.com/topic/63625-solved-functions-for-num-subdirectories-and-files-in-a-directory/#findComment-317076 Share on other sites More sharing options...
teng84 Posted August 6, 2007 Share Posted August 6, 2007 while (($file = readdir($dir)) !== false) { i think no need to condition for flase while ($file = readdir($dir)) { is ok why $file = readdir($dir) true if variable file get the value from readdir($dir) and false if not Quote Link to comment https://forums.phpfreaks.com/topic/63625-solved-functions-for-num-subdirectories-and-files-in-a-directory/#findComment-317078 Share on other sites More sharing options...
phpknight Posted August 7, 2007 Author Share Posted August 7, 2007 Once again, thanks for the input. Once difficulty with these options is that it still has to go through a loop. And if a directory has 10000 directories and 1 million files, that would be a time drain. I was hoping that PHP just had a function that called the system and got some number. Quote Link to comment https://forums.phpfreaks.com/topic/63625-solved-functions-for-num-subdirectories-and-files-in-a-directory/#findComment-317096 Share on other sites More sharing options...
teng84 Posted August 7, 2007 Share Posted August 7, 2007 ok maybe you will need this http://www.php.net/manual/en/function.scandir.php that result is in array now all you have to do is count the array Quote Link to comment https://forums.phpfreaks.com/topic/63625-solved-functions-for-num-subdirectories-and-files-in-a-directory/#findComment-317098 Share on other sites More sharing options...
phpknight Posted August 7, 2007 Author Share Posted August 7, 2007 At least that function is better. I was looking in the file functions. Thanks for the page. It still takes up a lot of memory with that array, though. I am surprised there is just not something simple that returns a number and doesn't have to loop. I thought operating systems would have something like that. Quote Link to comment https://forums.phpfreaks.com/topic/63625-solved-functions-for-num-subdirectories-and-files-in-a-directory/#findComment-317105 Share on other sites More sharing options...
teng84 Posted August 7, 2007 Share Posted August 7, 2007 is this solved? Quote Link to comment https://forums.phpfreaks.com/topic/63625-solved-functions-for-num-subdirectories-and-files-in-a-directory/#findComment-317110 Share on other sites More sharing options...
phpknight Posted August 7, 2007 Author Share Posted August 7, 2007 Well, I guess if that is the best answer, it is solved as much as it can be. I was looking for something more efficient, but your answers were very good. Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/63625-solved-functions-for-num-subdirectories-and-files-in-a-directory/#findComment-317114 Share on other sites More sharing options...
Fadion Posted August 7, 2007 Share Posted August 7, 2007 scandir() is for PHP5, so on a shared host which runs PHP4 u cant do much. Anyway u could try using system commands with exec(). U could use exec('tree', $arr) and count those lines with count($arr). Dont know if it is faster or not, but it can be worth the try. Quote Link to comment https://forums.phpfreaks.com/topic/63625-solved-functions-for-num-subdirectories-and-files-in-a-directory/#findComment-317120 Share on other sites More sharing options...
phpknight Posted August 7, 2007 Author Share Posted August 7, 2007 I have a managed dedicated with 5.2.3, so I should be good there. Quote Link to comment https://forums.phpfreaks.com/topic/63625-solved-functions-for-num-subdirectories-and-files-in-a-directory/#findComment-317168 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.