Hi,
I have a problem how to check my maximum file upload…
can only check the single image.
How can i check the whole file if it exceeded in 2mb.
Here is my script.
hile(list($key,$value) = each($_FILES["images"]["name"])){
if (($_FILES["images"]["size"][$key] >2097152))
{echo "<font color='#ff0000'>Maximum File Exceed</font>";}
else{
$filename = $value;
$add = "upload/$filename";
$imagename=($_FILES['images']['name'][$key]);
$original_name=$filename;
if(!empty($value))
{
while(file_exists($add)){
$filename = rand().$original_name;
$add = "upload/".$filename;
}
if(copy($_FILES[images][tmp_name][$key],$add)){
chmod("$add",0777);
}else{
echo "error";}
$i = $i+1;
$img[$i]=$filename;
}
}
}
Thanks.
You should look into filesize()
function format_bytes($bytes) {
if ($bytes < 1024) return $bytes.' B';
elseif ($bytes < 1048576) return round($bytes / 1024, 2).' KB';
elseif ($bytes < 1073741824) return round($bytes / 1048576, 2).' MB';
elseif ($bytes < 1099511627776) return round($bytes / 1073741824, 2).' GB';
else return round($bytes / 1099511627776, 2).' TB';
}
$filename = 'yourfile.avi';
echo format_bytes(filesize($filename));
PHP will fail if a file larger than the configured limits is uploaded. PHP will NOT fire your script so there is probably no way you can check if the file was uploaded or not.
Edit:
Having said that, if $_FILES[“images”][“size”][$key] contains the file size, just check:
if ($_FILES["images"]["size"][$key] > 2 * 1024 * 1024) { // 2 MB
// handle error
}
ah…so is there a way prevent to submit the file if there is a larger file one of the three that i upload.
PHP - Handling File Uploads (POST Method)
Take a look at the form fields there - that will at least subvert them trying to upload it… if you’re talking about pre-submission checks, that would be Javascript’s realm of influence.
Yes, but you’ll need to use something like SWFUpload although implementing it can be tricky.
Without using Flash or Java, there’s no way of checking how large a file is prior to submitting/ uploading it.
I think there’s some new HTML5 and Javascript stuff coming soon that might allow this, but I’m not 100% sure about this.