473,847 Members | 1,500 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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("Locatio n: /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_FIL ES['file']['size'] <=0)
{
print "<h2><b>You r 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_FIL ES['file']['size'] >5000k) or
if ($HTTP_POST_FIL ES['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 3741
"Jefferis NoSpamme" <je********@hot mail.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("Locatio n: /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_FIL ES['file']['size'] <=0)
{
print "<h2><b>You r 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_FIL ES['file']['size'] >5000k) or
if ($HTTP_POST_FIL ES['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_forma t($_FILES['liite']['size']/1024,1)." kb) is too big.
Please contact administration. ";
break;
case UPLOAD_ERR_PART IAL:
echo "File {$_FILES['liite']['name']} upload failed because file was
only partially transmitted. Please contact administration. ";
break;
case UPLOAD_ERR_NO_T MP_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_F ILE:
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.n ews.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_forma t($_FILES['liite']['size']/1024,1)." kb) is too big.
Please contact administration. ";
break;
case UPLOAD_ERR_PART IAL:
echo "File {$_FILES['liite']['name']} upload failed because file was
only partially transmitted. Please contact administration. ";
break;
case UPLOAD_ERR_NO_T MP_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_F ILE:
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********@hot mail.comwrote in message
news:C0******** *************** @hotmail.com...
On 7/3/06 3:23 AM, in article
VB************* *****@reader1.n ews.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_FO RM_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***@bhg byrzcv.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_FO RM_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($myfil e) 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_T MP_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_FO RM_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
7274
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 : some times, I receive a javascript error "Invalid syntax on line xxx", this line contains the call to submit as shown in the following lines : <input type="button" value="<%=Session("Bouton_EnregistrerFicheInclusion")%>"
8
18763
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 today, and I got a "file size limit exceeded" error.
14
4807
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 longer write to the file. When this happens, fopen and fputs do not return an error. I've been researching large file support for Linux, and it all has to do with the regular 2-gig file size limit. If it's something obvious, sorry -- I'm a C...
10
3840
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 MB) and after a while I get the Windows message saying that the virtual memory's getting really low. Plus the machine gets really sluggish (with multi-threading). Is it possible to use the virtual memory until it reaches a certain limit and then use...
9
1724
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 is the my test. How: Create a HTML page with 2 hyper links to these files (posted below). Right click, Save target As. Tested: On my P4 1.6 GHz , 1 GB Ram server, Windows 2000 IIS 5 and Apache 1.3 for win32, 100 Mbps LAN Sample Size: 150 MB...
3
13145
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 ³ and isn¹t checking the file size or preventing the upload. ERROR IS <<<<Œthis.form.uploadfile.value² is null or not an object on this line:
5
7072
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 columns each. When I try to select more rows I get a WSE352 "The size of the record exceed its limit." I think I am at the default 4 mb download limit. I am using WSE2.0 on dotNet Framework 1.1. I have tried changing the machine.config in the...
4
8473
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
4874
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
9892
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10995
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10347
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7888
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
7061
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5729
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5915
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4540
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4130
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.