Funker_joe Posted January 29, 2006 Share Posted January 29, 2006 Hello,I was wondering how I would go about having a number readout of how many files are in a given directory. Then that number would be displayed in the htm document.Sorry if this sound stupid, but I've never typed anything in PHP before.Thanks,-Joe Link to comment Share on other sites More sharing options...
aliendisaster Posted January 30, 2006 Share Posted January 30, 2006 Hello,I was wondering how I would go about having a number readout of how many files are in a given directory. Then that number would be displayed in the htm document.Sorry if this sound stupid, but I've never typed anything in PHP before.Thanks,-Joe<{POST_SNAPBACK}> Theres a couple different ways to do this. The best way for me to do it is with popen(). If your using linux, you can use my code.$totalFiles = -1;$handle = popen("ls path_to_your_directory_from_root", "r");while(!feof($handle)){ $line = fgets($handle); $totalFiles ++;} The popen runs the linux command ls for the directory in right mode. Then the while statement gets each line from that command and adds 1 to the counter. To get an acurate count, I had to set the counter to start at -1 (I'm not really sure why). You could also use opendir() and readdir() like so: $totalFiles = 0;$dirname = "relative_path_to_your_directory";$dh = opendir($dirname) or die("I can't find $dirname");while (!(($file = readdir($dh)) === false)){ if (is_file("$dirname/$file")){ $totalFiles ++; }}closedir($dh); This one works better on Windows. Link to comment Share on other sites More sharing options...
DeathRay2K Posted January 30, 2006 Share Posted January 30, 2006 If you're using PHP5+, you can do it this way. $dir = "path_to_directory";if($filesarray = scandir($dir)){ $numberoffiles = count($filesarray); echo $numberoffiles;}else{ echo "Failed to open directory.";} Link to comment Share on other sites More sharing options...
Funker_joe Posted January 31, 2006 Author Share Posted January 31, 2006 I get this error:Parse error: parse error, unexpected T_LNUMBER, expecting T_VARIABLE or '$' in /home/profile/public_html/watm/php/01.php on line 5 Link to comment Share on other sites More sharing options...
DeathRay2K Posted January 31, 2006 Share Posted January 31, 2006 What code did you use? Link to comment Share on other sites More sharing options...
Funker_joe Posted January 31, 2006 Author Share Posted January 31, 2006 What code did you use?<{POST_SNAPBACK}> I used the second code aliendisaster posted.Also, the php version installed is: 4.3.11<?php $totalFiles = 0;$dirname = "test01";if(!($dh=opendir($dirname))) die("Couldn't find ".$dirname);while (!(($file = readdir($dh)) === false)){ if (is_file("$test01/$file")){ $totalFiles ++; }}closedir($dh);?> I changed it to that.Then, I made the index file have: <a href="01122.htm"><?php echo "$totalFiles"; ?></a> Everything seems to be fine, but the link always shows up as 0, no matter if I add files to the dir or not. Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now