Page 1 of 1
limit an uploaded images file size
Posted: Wed Jun 24, 2009 12:32 pm
by tomsace
Hi,
I am trying to restrict an uploaded images file size to 500kb.
I have tried for hours now! Here is my current working image upload script (without limits):
Code: Select all
unset($imagename);
if(!isset($_FILES) && isset($HTTP_POST_FILES)) {
$_FILES = $HTTP_POST_FILES; }
if(!isset($_FILES['image_file'])) {
$imagename = basename($_FILES['image_file']['name']); }
if(empty($limit))
{ $newimage = "user/images/" . $username . ".jpg";
$result = @move_uploaded_file($_FILES['image_file']['tmp_name'], $newimage); }
I have tried adding a filesize() script, but when I upload an image and the the page refreshes, it doesn't reload the images file size until the the next refresh so it wasn't affective at all.
How else can I go about the task?
Re: limit an uploaded images file size
Posted: Wed Jun 24, 2009 12:35 pm
by iBroughtCookies
http://www.tizag.com/phpT/fileupload.php
You can actually do this through the HTML form:
Code: Select all
<input type="hidden" name="MAX_FILE_SIZE" value="500000" />
Re: limit an uploaded images file size
Posted: Wed Jun 24, 2009 12:40 pm
by tomsace
Thanks for the quick reply,
What would doing this method actually do??
What would I have to call in the PHP side??
Re: limit an uploaded images file size
Posted: Wed Jun 24, 2009 12:52 pm
by iBroughtCookies
Haha, you know to be honest, I'm not sure.
What I BELIEVE is that there's an HTML property called "MAX_FILE_SIZE" that can be set through the $_POST variable.
However, I have never tested it.
Here's a PHP based method:
Code: Select all
unset($imagename);
if(!isset($_FILES) && isset($HTTP_POST_FILES)) {
$_FILES = $HTTP_POST_FILES;
}
if(!isset($_FILES['image_file'])) {
$imagename = basename($_FILES['image_file']['name']);
}
$filesize = $_FILES["image_file"]["size"];
$maxsize = 64000;
if ($filesize > $maxsize) {
$filetoobig = 1;
}
if((empty($limit)) && (!$filetoobig)) {
$newimage = "user/images/" . $username . ".jpg";
$result = @move_uploaded_file($_FILES['image_file']['tmp_name'], $newimage);
}
Re: limit an uploaded images file size
Posted: Wed Jun 24, 2009 12:52 pm
by Benjamin
This should be done server side:
Code: Select all
64000 = 500kb
if ($_FILES['image_file']['size'] > 64000) {
// file too large
}
Re: limit an uploaded images file size
Posted: Thu Jun 25, 2009 5:18 pm
by tomsace
Thanks for the help.
I have tried adding the scripts provided but its just not uploading any image at all wether its more than 500kb or not.
It just echos File too large! on every upload.
Heres my script:
Code: Select all
unset($imagename);
if(!isset($_FILES) && isset($HTTP_POST_FILES)) {
$_FILES = $HTTP_POST_FILES; }
if(!isset($_FILES['image_file'])) {
$imagename = basename($_FILES['image_file']['name']); }
if ($_FILES['image_file']['size'] > 64000) {
$limit = "File too large!";}
if(empty($limit))
{ $newimage = "user/images/" . $username . ".jpg";
$result = @move_uploaded_file($_FILES['image_file']['tmp_name'], $newimage); }
echo "$limit"; }