ArshSingh Posted February 26, 2014 Share Posted February 26, 2014 i'm using the following code to calculate the size of directory uploads/username but know i want to tell to the script if directory username 's size is greater then 5 gb then upload function echo"Your sapce is full please dlete some files to proceed" so my final question is how do i tell php , if if limit exceeded then upload.php stop uploading files. <?PHP CLASS Directory_Calculator { VAR $size_in; VAR $decimals; FUNCTION calculate_whole_directory($directory) { IF ($handle = OPENDIR($directory)) { $size = 0; $folders = 0; $files = 0; WHILE (FALSE !== ($file = READDIR($handle))) { IF ($file != "." && $file != "..") { IF(IS_DIR($directory.$file)) { $array = $this->calculate_whole_directory($directory.$file.'/'); $size += $array['size']; $files += $array['files']; $folders += $array['folders']; } ELSE { $size += FILESIZE($directory.$file); $files++; } } } CLOSEDIR($handle); } $folders++; RETURN ARRAY('size' => $size, 'files' => $files, 'folders' => $folders); } FUNCTION size_calculator($size_in_bytes) { IF($this->size_in == 'B') { $size = $size_in_bytes; } ELSEIF($this->size_in == 'KB') { $size = (($size_in_bytes / 1024)); } ELSEIF($this->size_in == 'MB') { $size = (($size_in_bytes / 1024) / 1024); } ELSEIF($this->size_in == 'GB') { $size = (($size_in_bytes / 1024) / 1024) / 1024; } $size = ROUND($size, $this->decimals); RETURN $size; } FUNCTION size($directory) { $array = $this->calculate_whole_directory($directory); $bytes = $array['size']; $size = $this->size_calculator($bytes); $files = $array['files']; $folders = $array['folders'] - 1; // exclude the main folder RETURN ARRAY('size' => $size, 'files' => $files, 'folders' => $folders); } } ?> show the calculated size in mb <?PHP INCLUDE 'models/usersize.php'; /* Path to Directory - IMPORTANT: with '/' at the end */ $directory = "uploads/$loggedInUser->username/"; /* Calculate size in: B (Bytes), KB (Kilobytes), MB (Megabytes), GB (Gigabytes) */ $size_in = 'MB'; /* Number of decimals to show */ $decimals = 2; $directory_size = NEW Directory_Calculator; /* Initialize Class */ $directory_size->size_in = $size_in; $directory_size->decimals = $decimals; // return an array with: size, total files & folders $array = $directory_size->size($directory); ECHO "The directory <em>".$directory."</em> has a size of ".$array['size']." ".$size_in.", ".$array['files']." files & ".$array['folders']." folders."; ?> Quote Link to comment Share on other sites More sharing options...
Ch0cu3r Posted February 26, 2014 Share Posted February 26, 2014 (edited) You need to add the users directory size and the file size for the uploaded file together, if the total is less than 5242880 (5GB) then upload the file, else display error. Example $directory_size = new Directory_Calculator; $usersDirectoryFileSize = $directory_size->size($usersDirectoryPath); // calculate users storage space // NB: make sure $usersDirectoryFileSize['size'] returns the directory size in total bytes, not in english readable format like 1.2GB if(($usersDirectoryFileSize['size'] + $_FILES['file']['size']) < 5242880) { move_uploaded_file($_FILES['file']['tmp_name'], 'upload/destination/'.$_FILES['file']['name']); // add the file to the users storage directory } else { echo 'You have exceeded the 5GB storage space. Please delete some files'; } Edited February 26, 2014 by Ch0cu3r Quote Link to comment Share on other sites More sharing options...
jazzman1 Posted February 26, 2014 Share Posted February 26, 2014 Out of curiosity because I've never used php to setup a limit size for some particular directory. Where is the script to do this? Quote Link to comment Share on other sites More sharing options...
tunage Posted February 27, 2014 Share Posted February 27, 2014 Why don't you just adjust the upload limit size in your php.ini? Quote Link to comment Share on other sites More sharing options...
jazzman1 Posted February 27, 2014 Share Posted February 27, 2014 Why don't you just adjust the upload limit size in your php.ini? Most likely he wants to implement something similar to quota policies for users/members by using php, but not entirly sure 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.