|
Posted by Kimmo Laine on 07/03/06 07:23
"Jefferis NoSpamme" <jefferisp7@hotmail.com> wrote in message
news:C0CDEDB8.4424A%jefferisp7@hotmail.com...
> Hi all, I'm trying to limit the file size of an image submission and I
> keep
> running into various problems. I've got most of it working, but I'm
> stumped
> and I have a basic question as to WHY this works at all!
>
> if ($_FILES['file']['size'] !="") {
>
> if ($_FILES['file']['size']<=0) {
> header("Location: /fileerror.php");
> exit;
> }
> }
>
>
> What I want to do is skip this function IF the user submits no file. Part
> one works with the !="" condition, but two, which did work, does not when
> nested. It stops checking file sizes and submits every size file.
>
> I'm trying to modify Swanilda's tutorial: http://swanilda.com/unix2.html
>
> And using form file's input hidden value:
> Code:
> <input type="hidden" name="MAX_FILE_SIZE" value="5000" border="0">
Here's a magical line that tells user agent (browser) not to post files
larger than 5000. In this case $_FILES['file']['error'] should contain the
value of UPLOAD_ERR_FORM_SIZE .
> WITH this check:
> if ($HTTP_POST_FILES['file']['size'] <=0)
> {
> print "<h2><b>Your picture was not received.</b></h2><br>";
> print "The file size was larger than 5k.<br>";
> print "Reduce the size and resubmit.";
> }
>
>
> First of all, I don't want to stop the post if the field is empty, so that
> is problem 1. But I also don't understand why this code works. This code
> blocks an empty file [<=0], but why or how does it check to see that the
> file is not larger than that the max file size value of 5k? I don't see a
> comparative function or operator here. Why shouldn't you have to write
> something like:
> Code:
> if ($HTTP_POST_FILES['file']['size'] >5000k) or
> if ($HTTP_POST_FILES['file'] > ['size'] ) THEN do this?
>
>
> How is the size value placing a limit on the file size? This code is
> probably much simpler than I'm making it out to be, but I've been on 15
> sites trying to get this right, and most of the searches are for uploading
> files, which I'm not doing. I'm sending them via email.
>
>
> What I eventually want to do is just prevent submissions IF the user
> submits
> a file and it is larger than 3 megs...
> Thank YOU in advance,
> Jeff
If you want to hard limit to 3 megs, you can just adjust this:
<input type="hidden" name="MAX_FILE_SIZE" value="5000" border="0">
to
<input type="hidden" name="MAX_FILE_SIZE" value="3000" border="0">
If you wanna do really good errormessages, try something similar to what I
did with a file uploader. "liite" is the name of my file field:
switch($_FILES['liite']['error']){
case UPLOAD_ERR_INI_SIZE:
case UPLOAD_ERR_FORM_SIZE:
echo "File {$_FILES['liite']['name']} upload failed because the
filesize (".number_format($_FILES['liite']['size']/1024,1)." kb) is too big.
Please contact administration.";
break;
case UPLOAD_ERR_PARTIAL:
echo "File {$_FILES['liite']['name']} upload failed because file was
only partially transmitted. Please contact administration.";
break;
case UPLOAD_ERR_NO_TMP_DIR:
case UPLOAD_ERR_CANT_WRITE:
echo "File {$_FILES['liite']['name']} upload failed because of
server error. Please contact administration.";
break;
case UPLOAD_ERR_OK:
echo "File {$_FILES['liite']['name']} upload sucessful";
break;
case UPLOAD_ERR_NO_FILE:
default:
break;
}
And do check out the manual.
http://fi.php.net/manual/en/features.file-upload.php
[Back to original message]
|