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
tomsace
Forum Contributor
Posts: 167 Joined: Thu Jan 01, 2009 8:07 pm
Post
by tomsace » Wed Jun 24, 2009 12:32 pm
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?
tomsace
Forum Contributor
Posts: 167 Joined: Thu Jan 01, 2009 8:07 pm
Post
by tomsace » Wed Jun 24, 2009 12:40 pm
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
Post
by iBroughtCookies » Wed Jun 24, 2009 12:52 pm
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.
Benjamin
Site Administrator
Posts: 6935 Joined: Sun May 19, 2002 10:24 pm
Post
by Benjamin » Wed Jun 24, 2009 12:52 pm
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
Post
by tomsace » Thu Jun 25, 2009 5:18 pm
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"; }