Vivid Lust Posted November 30, 2007 Share Posted November 30, 2007 How could I account how many files there are in a directory and echo the amount? Thanks in advanced! Link to comment https://forums.phpfreaks.com/topic/79597-counting-files-in-a-directory/ Share on other sites More sharing options...
marcus Posted November 30, 2007 Share Posted November 30, 2007 <?php $count = count(glob("/path/to/your/directory/*")); # keep the asterick. echo $count; ?> Link to comment https://forums.phpfreaks.com/topic/79597-counting-files-in-a-directory/#findComment-403149 Share on other sites More sharing options...
Vivid Lust Posted November 30, 2007 Author Share Posted November 30, 2007 It deosnt work ??? <?php $count = count(glob("http://imgsurf.freehostia.com/uploads/*")); # keep the asterick. echo $count; ?> http://imgsurf.freehostia.com/count.php Link to comment https://forums.phpfreaks.com/topic/79597-counting-files-in-a-directory/#findComment-403157 Share on other sites More sharing options...
marcus Posted November 30, 2007 Share Posted November 30, 2007 Just do $count = count(glob("/uploads/*")); echo $count; Link to comment https://forums.phpfreaks.com/topic/79597-counting-files-in-a-directory/#findComment-403161 Share on other sites More sharing options...
Vivid Lust Posted November 30, 2007 Author Share Posted November 30, 2007 still 0 ..... Link to comment https://forums.phpfreaks.com/topic/79597-counting-files-in-a-directory/#findComment-403163 Share on other sites More sharing options...
Bramme Posted November 30, 2007 Share Posted November 30, 2007 are you sure the path is correct? else try with <?php $root = $_SERVER['DOCUMENT_ROOT']; $path= $root.'/uploads/'; $i = count(glob($path.'*')); echo $i; ?> Link to comment https://forums.phpfreaks.com/topic/79597-counting-files-in-a-directory/#findComment-403171 Share on other sites More sharing options...
Tjazi Posted November 30, 2007 Share Posted November 30, 2007 It deosnt work ??? <?php $count = count(glob("http://imgsurf.freehostia.com/uploads/*")); # keep the asterick. echo $count; ?> http://imgsurf.freehostia.com/count.php I was going to say you'll need your full path, which you can find by placing a file in your "uploads" folder called, lets say for instance, testblah.php with the contents: <?php prin/*t "hi"; ?> That should give an error, the path of the error that it gives is your full path, minus the filename of the file. Link to comment https://forums.phpfreaks.com/topic/79597-counting-files-in-a-directory/#findComment-403193 Share on other sites More sharing options...
The Little Guy Posted November 30, 2007 Share Posted November 30, 2007 <?php echo count(glob("uploads/*")); ?> Link to comment https://forums.phpfreaks.com/topic/79597-counting-files-in-a-directory/#findComment-403196 Share on other sites More sharing options...
GingerRobot Posted November 30, 2007 Share Posted November 30, 2007 If you wanted to count the number of files in a particular folder and all sub folders, you'd need to use recursion. Here's a blue peter-esque one i made earlier <?php function no_of_files($dir){ $no_of_files=0; $handler = opendir($dir); while(false !== ($file = readdir($handler))){ if($file != '.' && $file != '..'){ if(is_dir($dir.'\\'.$file)){//if this is a directory, recall the function with the subdirectory defined $sub_dir = $dir.'\\'.$file; $no_of_files += no_of_files($sub_dir); }else{//is a file, so increase our counter $no_of_files++; } } } return $no_of_files; } echo no_of_files('C:\Documents and Settings\Ben\My Documents\My Music'); ?> Link to comment https://forums.phpfreaks.com/topic/79597-counting-files-in-a-directory/#findComment-403198 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.