NorthWestSimulations Posted January 24, 2009 Share Posted January 24, 2009 Quick question for you guys. I have no clue on how to do this. and I got no clue where to look for help. So if you guys would help me at all by writing some source code or showing me a link to where I could go for information I would be greatly appreciative! How heres my problem: I have a folder in my cgi-bin called tmp. This is where I keep all my session data. Whenever I access my website a session is created that equals a filesize of 0kb. And whenever a session is registered depending on what kind of user they are the file size for the session file is around 1kb to 10kb. So heres my question... How would I make a script to count all the files that are equal to 0kb and store it to a variable $x. Then count all the files that are greater than or equal to 1kb and store the total in a variable $y. So that I could in turn create a thingy that says... There are $x Guests online, and $y Members online. Any suggestions? I hope I made egnouph sense... Quote Link to comment https://forums.phpfreaks.com/topic/142257-solved-files-in-directory-not-0kb/ Share on other sites More sharing options...
MadTechie Posted January 24, 2009 Share Posted January 24, 2009 This should do it <?php $dir = "cgi-bin/bin"; //FULL PATH $Guest = 0; $Members = 0; // Open a known directory, and proceed to read its contents if (is_dir($dir)) { if ($dh = opendir($dir)) { while (($file = readdir($dh)) !== false) { if($file == "." || $file == ".." || is_dir($dir.$file)) continue; $size = filesize($dir.$file); if($size > 0) { $Guest++; }else{ $Members++; } } closedir($dh); } } echo "Members: $Members<br>Guests: $Guest<br>"; Quote Link to comment https://forums.phpfreaks.com/topic/142257-solved-files-in-directory-not-0kb/#findComment-745313 Share on other sites More sharing options...
NorthWestSimulations Posted January 24, 2009 Author Share Posted January 24, 2009 Thank you bro. Works beautifully. Your a god... A PHPfreaks Recommended god Quote Link to comment https://forums.phpfreaks.com/topic/142257-solved-files-in-directory-not-0kb/#findComment-745346 Share on other sites More sharing options...
PFMaBiSmAd Posted January 24, 2009 Share Posted January 24, 2009 Using the existence of session data files to determine how many guest/members are on line is both inaccurate and inefficient. It's inaccurate because session data files are only removed when session garbage collection GC runs, and by default it runs randomly (to reduce the number of directory listings), so session data files can exist long after a visitor is no longer active on your site. GC also only removes files that are older than session.gc_maxlifetime so you cannot tell any closer than that value how long a go a guest/member was active and shortening the value will have the side affect of prematurely logging your members out. It's inefficient because on each page generation where you display the quantity of guests/members, you are accessing the disk directory, scanning through the files and accessing the file size of each file. You should instead use the traditional method of inserting/updating the last access time in a table of all the visitors (indicate guest/member) and then doing a simple query to get a count of each type that is less in the past than the time limit you pick. Quote Link to comment https://forums.phpfreaks.com/topic/142257-solved-files-in-directory-not-0kb/#findComment-745348 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.