hoopplaya4 Posted January 31, 2009 Share Posted January 31, 2009 Hi all, Let me do my best to explain the situation. I'm currently on a hosting plan and have a certain folder that users upload files onto through a web form. (For example, /home/users/public_html/uploads) I want to put a "cap" on the amount of disk space on that specific folder (uploads). I've spoken with my provider, and they cannot do it for me. So, my question is, how would I go about creating a script that, right before a user uploads, calculates the current space usage on the folder, compares it to my allowed usage (say 500 MB), and then either allows or disallows the upload based on their quota. Any point in the right direction would be fantastic! Thanks. Quote Link to comment Share on other sites More sharing options...
waynewex Posted January 31, 2009 Share Posted January 31, 2009 http://www.go4expert.com/forums/showthread.php?t=290 Quote Link to comment Share on other sites More sharing options...
printf Posted January 31, 2009 Share Posted January 31, 2009 Without reading, these are the fastest methods... Linux... <?php $f = './path/directory'; $io = popen ( '/usr/bin/du -sk ' . $f, 'r' ); $size = fgets ( $io, 4096); $size = substr ( $size, 0, strpos ( $size, '\t' ) ); pclose ( $io ); echo 'Directory: ' . $f . ' => Size: ' . $size; ?> Windows... <?php $f = 'f:/www/docs'; $obj = new COM ( 'scripting.filesystemobject' ); if ( is_object ( $obj ) ) { $ref = $obj->getfolder ( $f ); echo 'Directory: ' . $f . ' => Size: ' . $ref->size; $obj = null; } else { echo 'can not create object'; } Or if you don't have access to system calls you can use this... This gives the size based on the BLOCK SIZE, which is the actually disk space used. <? function dirsize ( $dir, $block = 4096 ) { $dirs = array ( $dir ); $size = array ( 0, 0 ); while ( isset ( $dirs[0] ) ) { $path = array_shift ( $dirs ); foreach ( glob ( $path . '/*' ) AS $next ) { if ( is_dir ( $next ) ) { $dirs[] = $next; } else { $fs = filesize ( $next ); $size[0] += $fs; if ( $fs != 0 && $fs <= $block ) { $size[1] += $block; } else if ( $fs > $block ) { $ts = ceil ( ( $fs / $block ) ); $size[1] += ( $block * $ts ); } } } } return $size; } /* no trailing '/' */ $path = '.'; // array[0] = total file size, array[1] = total disk space used, (for the directory) print_r ( dirsize ( $path ) ); ?> Quote Link to comment Share on other sites More sharing options...
hoopplaya4 Posted January 31, 2009 Author Share Posted January 31, 2009 Thanks printf and waynewex, both were exactly what I was looking for! Quote Link to comment 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.