PDA

View Full Version : Limiting upload size



g_force
09-27-2008, 04:48 PM
Can anyone tell me how to limit the size of my uploads? This is within the coding (below) but doesn't seem to be working enabling uploads of all sizes. Ideally as well I would like to specify the error like the others, e.g. "the file is too big" so that users know the problem.

Could it be anything to do with my php settings and if so how can I change them?_regards


<?php
if (($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpg")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/pjpeg")
&& ($_FILES["file"]["size"] < 200))
{
if ($_FILES["file"]["error"] > 0)
{
echo "Return Code: " . $_FILES["file"]["error"] . "<br />";
}
else
{
echo "Upload: " . $_FILES["file"]["name"] . "<br />";
echo "Type: " . $_FILES["file"]["type"] . "<br />";
echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";
echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br />";

if (file_exists("upload/" . $_FILES["file"]["name"]))
{
echo $_FILES["file"]["name"] . " already exists. ";
}
else
{
move_uploaded_file($_FILES["file"]["tmp_name"],
"upload/" . $_FILES["file"]["name"]);
echo "Stored in: " . "upload/" . $_FILES["file"]["name"];
}
}
}
else
{
echo "Invalid file";
}
?>

Nile
09-28-2008, 01:45 AM
Ok, well first of all, if your uploading images, 200 kb just isn't gonna do it - sorry.
Also, in your php.ini file you should have something like this:



;;;;;;;;;;;;;;;;
; File Uploads ;
;;;;;;;;;;;;;;;;

; Whether to allow HTTP file uploads.
file_uploads = On

; Temporary directory for HTTP uploaded files (will use system default if not
; specified).
upload_tmp_dir = "c:/wamp/tmp"

; Maximum allowed size for uploaded files.
upload_max_filesize = 3000m
The highlighted part may not be what you have, change it to something bigger. Also make sure that the file is also uploading.
Put a link to your page please if none of those works.

g_force
09-28-2008, 10:53 AM
200 was just an example.. obviously I will allow more but for testing purposes I set it to that.

The upload_max_filesize is currently 35M. How can I change these settings? The uploading is working but without restricting the file size. I've put the current code below but I'm worried about putting a test page up as anyone could upload anything to my server.


<?php
if (($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpg")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/pjpeg")
)
{
if ($_FILES["file"]["error"] > 0)
{
echo "Return Code: " . $_FILES["file"]["error"] . "<br />";
}
else
{
if($_FILES["file"]["size"] > 2){
echo 'the file is too big';
}else{
echo "Upload: " . $_FILES["file"]["name"] . "<br />";
echo "Type: " . $_FILES["file"]["type"] . "<br />";
echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";
echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br />";
}
if (file_exists("upload/" . $_FILES["file"]["name"]))
{
echo $_FILES["file"]["name"] . " already exists. ";
}
else
{
move_uploaded_file($_FILES["file"]["tmp_name"],
"upload/" . $_FILES["file"]["name"]);
echo "Stored in: " . "upload/" . $_FILES["file"]["name"];
}
}
}
else
{
echo "Invalid file";
}
?>

Nile
09-28-2008, 12:34 PM
What PHP version do you have?

g_force
09-28-2008, 02:06 PM
5.2.6

g_force
09-30-2008, 08:26 PM
Any ideas anyone??