Jump to content

Limiting upload sizes


cs1h

Recommended Posts

There are two ways I can figure out how to do this.

1. Limit after upload to dimensions.

2. Limit before upload to file size.

 

First 1:

Let's assume that you've stored the upload in "uploads/image.jpg" and want to limit the size to 640x480.

 

$imageSize = getimagesize("uploads/image.jpg");
if($imageSize[0] > 640 || $imageSize[1] > 480)
{
echo "invalid image: too big";
unlink("uploads/image.jpg");
}
else
{
echo "Image OK.";
}

 

This is a suboptimal limitation method though, because the file has already been uploaded! To limit file size to 10KB, use code something like this in your upload script prior to storing the file.

 

if($_FILES["imgFile"]["size"] < 10000)
{
echo "file too big";
}
else
{
echo "file OK";
move_uploaded_file($_FILES["file"]["tmp_name"], "uploads/" . $_FILES["file"]["name"]);
}

 

That should work.

Link to comment
https://forums.phpfreaks.com/topic/70806-limiting-upload-sizes/#findComment-356025
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.