directory quota

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
hongco
Forum Contributor
Posts: 186
Joined: Sun Feb 20, 2005 2:49 pm

directory quota

Post by hongco »

anyone has a good solution on how to limit directory's space. If a user uploads more than a maximum amount, he/she will be denied. Intead of having a file reading total bytes in that directory, is there any other to set it to a max size?


Thanks!
User avatar
Skara
Forum Regular
Posts: 703
Joined: Sat Mar 12, 2005 7:13 pm
Location: US

Post by Skara »

If the user can only upload from a browser, you can simply read the filesize of all the files recursively, easy as can be.
If they have ftp/ssh access, I haven't a clue; I'd like to know too.
hongco
Forum Contributor
Posts: 186
Joined: Sun Feb 20, 2005 2:49 pm

Post by hongco »

i have already mentioned your method :) Thanks though. I doublt there is any.
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

Not sure if it works on windows but if you're using *nix then make it a home dir (or subdir of one) for a user with limited quota.

If its not your server then I'd go with your first approach... use a recursive function to check out all the file sizes and give you the total file size.

Something like this but I'm completely blotto cos it's 2:16am and I'm at work soon so need some sleep so someone should check it over first.

Code: Select all

function size_recursive($path, $s=0) {
	if (is_dir($path)) {
		if ($path != '.' && $path !== '..') {
			$h = opendir($path);
			while ($f = readdir($h)) {
				if (is_dir($path.'/'.$f) && $f != '.' && $f != '..') {
					$s = size_recursive($path.'/'.$f, $s);
				} else {
					$s += filesize($path.'/'.$f);
				} //End if
			} //End while
			closedir($h);
		} //End if
	} else {
		$s += filesize($path);
	} //End if
	return $s;
} //End function

//Example
$dir = 'c:/webs';
echo size_recursive($dir); //Total size in bytes of c:/webs
hongco
Forum Contributor
Posts: 186
Joined: Sun Feb 20, 2005 2:49 pm

Post by hongco »

thanks d11wtq,

I probably just go with the method you and Skara mentioned, that way the script can be used on any *nix hosting service, and its purpose is to provide members with some web space to upload things to. Thank you for your snippet too.

Here is a cup of coffe to keep your eyes wide open on the road to work:
[_]? <--- capuchino :lol: ..on second thought, 2am and going to work, I might let you have a big bowl of coffee - have you ever had one? haha \____/
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

As already mentionned, for EXT2 etc you want to read "man quota".
But if i'm not mistaken, on NTFS you can also apply quotas.. (http://www.microsoft.com/resources/docu ... o_lvms.asp)
Post Reply