limit an uploaded images file size

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
tomsace
Forum Contributor
Posts: 167
Joined: Thu Jan 01, 2009 8:07 pm

limit an uploaded images file size

Post 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?
iBroughtCookies
Forum Newbie
Posts: 10
Joined: Wed Jun 24, 2009 12:09 pm

Re: limit an uploaded images file size

Post 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" />
Last edited by Benjamin on Wed Jun 24, 2009 12:49 pm, edited 1 time in total.
Reason: Changed code type from text to html.
tomsace
Forum Contributor
Posts: 167
Joined: Thu Jan 01, 2009 8:07 pm

Re: limit an uploaded images file size

Post by tomsace »

Thanks for the quick reply,
What would doing this method actually do??

What would I have to call in the PHP side??
iBroughtCookies
Forum Newbie
Posts: 10
Joined: Wed Jun 24, 2009 12:09 pm

Re: limit an uploaded images file size

Post 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); 
}
 
Last edited by iBroughtCookies on Wed Jun 24, 2009 12:54 pm, edited 2 times in total.
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Re: limit an uploaded images file size

Post by Benjamin »

This should be done server side:

Code: Select all

 
64000 = 500kb
if ($_FILES['image_file']['size'] > 64000) {
// file too large
}
 
tomsace
Forum Contributor
Posts: 167
Joined: Thu Jan 01, 2009 8:07 pm

Re: limit an uploaded images file size

Post 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"; }
Post Reply