Jump to content

Upload.php don't upload if limit exceed


ArshSingh

Recommended Posts

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.";
?>
Link to comment
Share on other sites

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 by Ch0cu3r
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.