473,416 Members | 1,734 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,416 software developers and data experts.

Limit File Size in email form??

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">

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
_________________
----== Posted via Newsfeeds.Com - Unlimited-Unrestricted-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption =----
Jul 3 '06 #1
5 3702
"Jefferis NoSpamme" <je********@hotmail.comwrote in message
news:C0***********************@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

Jul 3 '06 #2
On 7/3/06 3:23 AM, in article VB******************@reader1.news.jippii.net,
"Kimmo Laine" <sp**@outolempi.netwrote:

Thank you for the help Kimmo.
>
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 .
I have read that the browser function on file size is not reliable and
works only on some browsers. I think I'm having a problem understanding how
the Max_File_Size value field is functioning and what it communicates to the
server. I looked up that reserved word on php and it isn't a php function,
but apparently is a server html function... Is the error value added by a
$var definition on the php page or is it fed to the php target by the Max
File form field???

Thanks
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
~~~~~~~~~~~~
Jefferis Peterson, Pres.
Web Design and Marketing
http://www.PetersonSales.com

----== Posted via Newsfeeds.Com - Unlimited-Unrestricted-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption =----
Jul 3 '06 #3
"Jefferis NoSpamme" <je********@hotmail.comwrote in message
news:C0***********************@hotmail.com...
On 7/3/06 3:23 AM, in article
VB******************@reader1.news.jippii.net,
"Kimmo Laine" <sp**@outolempi.netwrote:

Thank you for the help Kimmo.
>>
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 .
I have read that the browser function on file size is not reliable and
works only on some browsers. I think I'm having a problem understanding
how
the Max_File_Size value field is functioning and what it communicates to
the
server. I looked up that reserved word on php and it isn't a php function,
but apparently is a server html function... Is the error value added by a
$var definition on the php page or is it fed to the php target by the Max
File form field???
Assuming your file field looks something like this:
<input type="file" name="myfile">
The error code is, like I explained the first time, in the $_FILES array
under $_FILES['myfile']['error'].

if( $_FILES['myfile']['error']==UPLOAD_ERR_FORM_SIZE ){
echo "too big file";
}

You also get the size of the file in $_FILES['myfile']['size'] which you can
test to see if it's too big. In your case 3 megs = 3*1024*1024 bytes =
3145728

if( $_FILES['myfile']['size'] 3145728 ){
echo "too big file";
}

--
"ohjelmoija on organismi joka muuttaa kofeiinia koodiksi" -lpk
sp**@outolempi.net | Gedoon-S @ IRCnet | rot13(xv***@bhgbyrzcv.arg)
Jul 4 '06 #4
On 7/4/06 2:21 AM, in article 6O*******************@reader1.news.jippii.net,
"Kimmo Laine" <sp**@outolempi.netwrote:
The error code is, like I explained the first time, in the $_FILES array
under $_FILES['myfile']['error'].

Thank you. That was the missing piece to my understanding. I didn't realize
these file's values were seen automatically as an array. I was interpreting
the $_Files['size'] as a value passed from the form Max_File_Size field's
hidden variable and not from the file itself.
~~~~~~~~~~~~
Jefferis Peterson, Pres.
Web Design and Marketing
http://www.PetersonSales.com

----== Posted via Newsfeeds.Com - Unlimited-Unrestricted-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption =----
Jul 4 '06 #5
On 7/4/06 2:21 AM, in article 6O*******************@reader1.news.jippii.net,
"Kimmo Laine" <sp**@outolempi.netwrote:
Assuming your file field looks something like this:
<input type="file" name="myfile">
The error code is, like I explained the first time, in the $_FILES array
under $_FILES['myfile']['error'].

if( $_FILES['myfile']['error']==UPLOAD_ERR_FORM_SIZE ){
echo "too big file";
}

Thank you again Kimmo. I got it to work finally. The problem I believe is
that I was trying to use filesize($myfile) and other functions which have
to do with a file that is already uploaded to the server. When I tried to
use filesize() to the file, the error came back " no such file."

When I tried to apply your code straight, it failed on the server error:
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.";

I assume the problem is that I have not given permissions for a file upload
to reside on the server, as that was not my goal. I'm not entirely sure how
this pass through function works with email, but I believe it is only a
temporary directory, and it is handled automatically by my server. So I
could not check file sizes with a lot of the functions in the uploading a
file section of php manual. It has to be done on the fly, so to speak, and
yes your code:
if( $_FILES['myfile']['error']==UPLOAD_ERR_FORM_SIZE ){
> echo "too big file";
}
Worked perfectly. Thanks again,

Jeff
~~~~~~~~~~~~
Jefferis Peterson, Pres.
Web Design and Marketing
http://www.PetersonSales.com

----== Posted via Newsfeeds.Com - Unlimited-Unrestricted-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption =----
Jul 4 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Gerard Cany | last post by:
I have a problem with a javascript that seems related to the number/size of parameters sent by the form The method for the form is POST (not GET, I know the size limit with GET) The symptom :...
8
by: Peter Ballard | last post by:
Hi all, I've got a C program which outputs all its data using a statement of the form: putchar(ch, outfile); This has worked fine for years until it had to output more than 2GB of data...
14
by: Aaron Couts | last post by:
I have a program that writes to a log file. It's compiled on RH Linux 7.3 (Kernel 2.4.18-18.7). It's using fopen in append mode. When the file reaches 51200000 bytes in size, the program will no...
10
by: VM | last post by:
How can I limit the use of the PC's virtual memory? I'm running a process that basically takes a txt file and loads it to a datatable. The problem is that the file is over 400,000 lines long (77...
9
by: James Macbell | last post by:
I think I have pushed ASP.NET to the limit, I am not sure if I have done anything wrong in the code because I am trying to make 2 pieces of code (C# vs PHP) using the same algorithm. Anyways, here...
3
by: Jefferis NoSpamme | last post by:
Hello all, I'm trying to limit the file size to 1 meg on upload of image files and I am trying a script from javascript internet, but it is giving me errors on IE ² is null or not an object ³...
5
by: Steve | last post by:
WSE352 Size of the record exceed its limit I have a C#.Net windows app that calls a FileNet web service. I can run a select against the web service and it returns up to 7,200 records with 5...
4
by: Alec MacLean | last post by:
Is anyone aware of a size limit imposed on the subject text when using the System.Net.Mail library? I'm getting problems of message not being recieved if the subject exceeds 15 chars. Thx
6
by: howa | last post by:
Suppose the file is stored in "upload_tmp_dir ", so why I need to increase the memory limit? If I want to upload 100 MB, how large should I set? Thanks.
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.