Reputation: 530
I am trying to upload something using PHP and set a limit on the total size that I allow to be uploaded. I want to limit my uploads to 2MB but for some reason whenever I try to check with an if statement like this:
if (($_FILES["file"]["size"] < 2097152))
A file that is large (such as a 7mb file) will pass through the if statement because for whatever reason if I print $_FILES["file"]["size"]
, it will return 0, instead of the proper number of bytes. If I try to upload something that is smaller, like 342kb the $_FILES["file"]["size"]
will return the proper size.
Is there anyway to get $_FILES["file"]["size"]
to actually hold the proper size of the file? Otherwise I do not know how to fix this problem.
Upvotes: 24
Views: 28520
Reputation: 705
check for any errors prior to uploading. These will often give away what the problem is. Create a class to return any code error and use it for uploading files.
class UploadException extends Exception
{
public function __construct($code) {
$message = $this->codeToMessage($code);
parent::__construct($message, $code);
}
private function codeToMessage($code)
{
switch ($code) {
case UPLOAD_ERR_INI_SIZE:
$message = "The uploaded file exceeds the upload_max_filesize directive in php.ini.";
break;
case UPLOAD_ERR_FORM_SIZE:
$message = "The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form";
break;
case UPLOAD_ERR_PARTIAL:
$message = "The uploaded file was only partially uploaded";
break;
case UPLOAD_ERR_NO_FILE:
$message = "No file was uploaded";
break;
case UPLOAD_ERR_NO_TMP_DIR:
$message = "Missing a temporary folder";
break;
case UPLOAD_ERR_CANT_WRITE:
$message = "Failed to write file to disk";
break;
case UPLOAD_ERR_EXTENSION:
$message = "File upload stopped by extension";
break;
default:
$message = "Unknown upload error";
break;
}
return $message;
}
}
Now check whether the upload was success before you do anything with the file. If it's NOT uploaded successfully, it'll result in a error which will tell you what's wrong.
This way, instead of having to waste your time on guessing what the error code could mean or having to look it up all the time, your own made up error message will return the message corresponding with the error code.
if ($_FILES['realFile']['error'] === UPLOAD_ERR_OK) {
echo 'no problems encountered. File was uploaded with success';
} else {
throw new UploadException($_FILES['realFile']['error']);
}
Upvotes: 1
Reputation: 324640
if( $_FILES['file']['size'] && $_FILES['file']['size'] < (2<<20))
Try that.
<< is bitwise shift operator, decimal 2 is binary "10", then add 20 zeros.
Upvotes: 4
Reputation: 360682
A file which aborts for any reason (upload failed, exceeds limits, etc...) will show as size 0
You have to check for upload SUCCESS before you do ANYTHING with the rest of th eupload data:
if(array_key_exists('file', $_FILES)){
if ($_FILES['file']['error'] === UPLOAD_ERR_OK) {
echo 'upload was successful';
} else {
die("Upload failed with error code " . $_FILES['file']['error']);
}
}
The error codes are defined here. In your case, if you've hardcoded a 2meg limit and someone uploads a 2.1 meg file, then the error code would be UPLOAD_ERR_INI_SIZE (aka 2
), which is "exceeds limit set in .ini file".
Upvotes: 41
Reputation: 57573
How I supposed in my previous comment, your problem is that limit of uploadable file in php.ini is less than 7MB.
So you could try to use
if ($_FILES["file"]["size"] > 0 && $_FILES["file"]["size"] < 2097152)
Consider that if you put your limit (in php.ini) to 2MB, that check could be easily written as
if ($_FILES["file"]["size"] > 0)
Upvotes: 1