0

I am a novice in PHP, so sorry if the question sounds stupid.

I was wondering if there is a way to restrict file upload through PHP when the uploads folder size (not file size) is more than let's say 10 GB. If there is a way to do it, can you please provide me a concept of how to achieve this.

I am currently using POST to upload file and the upload folder is on my Hostgator server.

Thank You!

PeeJay
  • 323
  • 4
  • 16

3 Answers3

0

A similar question has been answered here. Just check the directory size before moving the temporary file into it. You need the linux answer, not the windows one.

Community
  • 1
  • 1
alez007
  • 276
  • 1
  • 6
0

The following code will calculate the total size of all files in a folder therefore giving you total folder size

<?php
$path = "gal";
echo "Folder $path = ".filesize_r($path)." bytes";

function filesize_r($path){
   if(!file_exists($path)) return 0;
   if(is_file($path)) return filesize($path);
     $ret = 0;
     foreach(glob($path."/*") as $fn)
     $ret += filesize_r($fn);
     return $ret;
   }
 ?>

Then you can build an if statement before the upload that will check the file size and if it is larger than 10gb, alert the user that the upload cannot be processed.

EDIT:

The IF-statement will look something like this

$filesize = filesize_r($path)

if $filesize > 10 000 000 000 {
    echo "Upload cannot be processed";
} else {
    uploadFile()
}
Lemuel Botha
  • 659
  • 8
  • 22
  • Thank You. I am going to try this, will let you know. – PeeJay Jul 18 '14 at 08:29
  • the code works great... Now trying to setup the if command. Thanks. – PeeJay Jul 18 '14 at 08:52
  • Please be aware that this function scans through all files of the directory and sums the filesizes and since we're talking about 10Gb directory, having many files will make it slow. – alez007 Jul 18 '14 at 09:02
  • 1
    @user1017334 The OP says he wants to restrict uploads once the directory reaches 10gb, so the function needs to scan through all files of the directory and sum the total file sizes – Lemuel Botha Jul 18 '14 at 09:04
0

First check the directory size like this.

Then check file size:

$file_size = $_FILES['file']['size'];

Then check if the file will fit and act accordingly:

if (foldersize($path) + $file_size > "10000000000")
{
  Tell user it doesn't fit
}
else
{
  Upload file function
}
Community
  • 1
  • 1
intTiger
  • 31
  • 3