473,796 Members | 2,741 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Checking File Size Before Upload

Hi all,

I am not a developer, but I work with several and am posting this for
them. We are trying to determine whether it is possible to check the
size of a file before a user uploads it.

Everything I've read suggests that this is impossible (or at least very
difficult) in PHP, and this makes sense for a server-side language.
What gives me pause, however, is the fact that when a user tries to
upload a file larger than the max file size we allow, an error appears
in our error log immediately -- yet the upload continues.

So somehow PHP must be recognizing that the file is too big well before
it's been uploaded. The question is how -- and how do we make the leap
from that recognition to an immediate halt of the upload?

Aug 22 '06 #1
19 9275
On 22 Aug 2006 14:59:35 -0700, "jeremyz" <je*********@gm ail.comwrote:
>I am not a developer, but I work with several and am posting this for
them. We are trying to determine whether it is possible to check the
size of a file before a user uploads it.

Everything I've read suggests that this is impossible (or at least very
difficult) in PHP, and this makes sense for a server-side language.
What gives me pause, however, is the fact that when a user tries to
upload a file larger than the max file size we allow, an error appears
in our error log immediately -- yet the upload continues.

So somehow PHP must be recognizing that the file is too big well before
it's been uploaded. The question is how -- and how do we make the leap
from that recognition to an immediate halt of the upload?
The browser may send a Content-length header with the file upload. PHP can
read this immediately, since it's right at the start of the request, and so can
produce an error in the log if it's too big.

However, there's no way for PHP to send a response to the browser yet, because
the content of the file is part of the HTTP request. Its choices are to either
unceremoniously dump the connection without a response (which it doesn't do,
for obvious reasons, although the HTTP protocol does allow this), or wait until
the request has finished so it can send a response back with an error message.

AFAIK this is a limitation of the HTTP protocol and so cannot be worked
around.

--
Andy Hassall :: an**@andyh.co.u k :: http://www.andyh.co.uk
http://www.andyhsoftware.co.uk/space :: disk and FTP usage analysis tool
Aug 22 '06 #2
Andy Hassall wrote:
However, there's no way for PHP to send a response to the browser yet, because
the content of the file is part of the HTTP request. Its choices are to either
unceremoniously dump the connection without a response (which it doesn't do,
for obvious reasons, although the HTTP protocol does allow this), or wait until
the request has finished so it can send a response back with an error message.

AFAIK this is a limitation of the HTTP protocol and so cannot be worked
around.
I don't think the HTTP protocol specs mandates that a response can only
be sent after the request body has been fully received. In theory, the
server can send status code 100 to accept the request or a 4xx error
code to reject it. That's not how it's implemented in the browsers
though, AFAIK.

The browser should pass the size of the request to onSubmit if you ask
me. Sometimes I wish HTML hadn't just stopped progressing completely...

Aug 23 '06 #3
Andy Hassall wrote:
However, there's no way for PHP to send a response to the browser yet, because
the content of the file is part of the HTTP request. Its choices are to either
unceremoniously dump the connection without a response (which it doesn't do,
for obvious reasons, although the HTTP protocol does allow this), or wait until
the request has finished so it can send a response back with an error message.

AFAIK this is a limitation of the HTTP protocol and so cannot be worked
around.
I don't think the HTTP protocol specs mandates that a response can only
be sent after the request body has been fully received. In theory, the
server can send status code 100 to accept the request or a 4xx error
code to reject it. That's not how it's implemented in the browsers
though, AFAIK.

The browser should pass the size of the request to onSubmit if you ask
me. Sometimes I wish HTML hadn't just stopped progressing completely...

Aug 23 '06 #4
Chung Leong wrote:
Andy Hassall wrote:
> However, there's no way for PHP to send a response to the browser yet, because
the content of the file is part of the HTTP request. Its choices are to either
unceremoniousl y dump the connection without a response (which it doesn't do,
for obvious reasons, although the HTTP protocol does allow this), or wait until
the request has finished so it can send a response back with an error message.

AFAIK this is a limitation of the HTTP protocol and so cannot be worked
around.

I don't think the HTTP protocol specs mandates that a response can only
be sent after the request body has been fully received. In theory, the
server can send status code 100 to accept the request or a 4xx error
code to reject it. That's not how it's implemented in the browsers
though, AFAIK.

The browser should pass the size of the request to onSubmit if you ask
me. Sometimes I wish HTML hadn't just stopped progressing completely...
Hi,

try using a hidden max file size field to pass to php

<input type="hidden" name="MAX_FILE_ SIZE" value="2048576" >

Cheers,

Luke.

Aug 23 '06 #5
Luke - ea********@gmai l.com wrote:
Chung Leong wrote:
>Andy Hassall wrote:
>> However, there's no way for PHP to send a response to the browser
yet, because
the content of the file is part of the HTTP request. Its choices are
to either
unceremonious ly dump the connection without a response (which it
doesn't do,
for obvious reasons, although the HTTP protocol does allow this), or
wait until
the request has finished so it can send a response back with an error
message.

AFAIK this is a limitation of the HTTP protocol and so cannot be worked
around.

I don't think the HTTP protocol specs mandates that a response can only
be sent after the request body has been fully received. In theory, the
server can send status code 100 to accept the request or a 4xx error
code to reject it. That's not how it's implemented in the browsers
though, AFAIK.

The browser should pass the size of the request to onSubmit if you ask
me. Sometimes I wish HTML hadn't just stopped progressing completely...

Hi,

try using a hidden max file size field to pass to php

<input type="hidden" name="MAX_FILE_ SIZE" value="2048576" >

Cheers,

Luke.
Just found a reference to it. http://uk2.php.net/features.file-upload
Aug 23 '06 #6
On 22 Aug 2006 20:38:44 -0700, "Chung Leong" <ch***********@ hotmail.comwrot e:
>I don't think the HTTP protocol specs mandates that a response can only
be sent after the request body has been fully received
Isn't it implied by HTTP 1.1 sec 6:

http://www.w3.org/Protocols/rfc2616/...sec6.html#sec6
"6 Response

After receiving and interpreting a request message, a server responds with an
HTTP response message. "

The key being "After"?

--
Andy Hassall :: an**@andyh.co.u k :: http://www.andyh.co.uk
http://www.andyhsoftware.co.uk/space :: disk and FTP usage analysis tool
Aug 23 '06 #7
Andy Hassall wrote:
On 22 Aug 2006 20:38:44 -0700, "Chung Leong" <ch***********@ hotmail.comwrot e:
I don't think the HTTP protocol specs mandates that a response can only
be sent after the request body has been fully received

Isn't it implied by HTTP 1.1 sec 6:

http://www.w3.org/Protocols/rfc2616/...sec6.html#sec6
"6 Response

After receiving and interpreting a request message, a server responds with an
HTTP response message. "

The key being "After"?
But the existence of status code 100 implies a interim response can be
sent. Look at 8.2.3:

The purpose of the 100 (Continue) status (see section 10.1.1) is to
allow a client that is sending a request message with a request body to
determine if the origin server is willing to accept the request (based
on the request headers) before the client sends the request body. In
some cases, it might either be inappropriate or highly inefficient for
the client to send the body if the server will reject the message
without looking at the body.

[http://www.w3.org/Protocols/rfc2616/...html#sec8.2.3]

So the desired behavior is defined in the specs. I don't know if it's
widely implemented though.

Aug 23 '06 #8
On 23 Aug 2006 13:17:34 -0700, "Chung Leong" <ch***********@ hotmail.comwrot e:
>Andy Hassall wrote:
>On 22 Aug 2006 20:38:44 -0700, "Chung Leong" <ch***********@ hotmail.comwrot e:
>I don't think the HTTP protocol specs mandates that a response can only
be sent after the request body has been fully received

Isn't it implied by HTTP 1.1 sec 6:

http://www.w3.org/Protocols/rfc2616/...sec6.html#sec6
"6 Response

After receiving and interpreting a request message, a server responds with an
HTTP response message. "

The key being "After"?

But the existence of status code 100 implies a interim response can be
sent. Look at 8.2.3:

The purpose of the 100 (Continue) status (see section 10.1.1) is to
allow a client that is sending a request message with a request body to
determine if the origin server is willing to accept the request (based
on the request headers) before the client sends the request body. In
some cases, it might either be inappropriate or highly inefficient for
the client to send the body if the server will reject the message
without looking at the body.

[http://www.w3.org/Protocols/rfc2616/...html#sec8.2.3]

So the desired behavior is defined in the specs. I don't know if it's
widely implemented though.
Ah! Interesting - thanks.

So PHP could - in theory - send the response back as a 417 and populate
$_FILES with a suitable error.

--
Andy Hassall :: an**@andyh.co.u k :: http://www.andyh.co.uk
http://www.andyhsoftware.co.uk/space :: disk and FTP usage analysis tool
Aug 23 '06 #9
Luke - ea********@gmai l.com wrote:
Luke - ea********@gmai l.com wrote:
>Chung Leong wrote:
>>Andy Hassall wrote:
However, there's no way for PHP to send a response to the browser
yet, because
the content of the file is part of the HTTP request. Its choices are
to either
unceremoniou sly dump the connection without a response (which it
doesn't do,
for obvious reasons, although the HTTP protocol does allow this), or
wait until
the request has finished so it can send a response back with an
error message.

AFAIK this is a limitation of the HTTP protocol and so cannot be
worked
around.

I don't think the HTTP protocol specs mandates that a response can only
be sent after the request body has been fully received. In theory, the
server can send status code 100 to accept the request or a 4xx error
code to reject it. That's not how it's implemented in the browsers
though, AFAIK.

The browser should pass the size of the request to onSubmit if you ask
me. Sometimes I wish HTML hadn't just stopped progressing completely...

Hi,

try using a hidden max file size field to pass to php

<input type="hidden" name="MAX_FILE_ SIZE" value="2048576" >

Cheers,

Luke.
Just found a reference to it. http://uk2.php.net/features.file-upload
Not sure if this holds truth in general but: on my host's
(Centos/Apache2.0.50/PHP4.3.4) server, using firefox as a browser (on
Gentoo) the MAX_FILE_SIZE field is not acknowledged by either the server
OR the browser. It simply sends the request regardless and than we're
back at OP's original issue.

I believe some choose a javascript solution to have the client check the
filesize. Not sure how (well) this works.

Sh
Aug 23 '06 #10

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

Similar topics

2
3932
by: matt | last post by:
I have compiled some code, some written by me, some compiled from various sources online, and basically i've got a very simple flat file photo gallery. An upload form, to upload the photos and give them a caption, storing the caption and filename in a text file. It's a bit buggy when removing the photos and captions from the file, and also in displaying them on the delete page. you can see it in action at www.4am.com.au/gallery/upload.php...
5
5469
by: Dave Smithz | last post by:
Hi There, I have a PHP script that sends an email with attachment and works great when provided the path to the file to send. However this file needs to be on the same server as the script. I want to develop a webpage where people can send attachments that are stored on their local PC.
1
2767
by: BW | last post by:
I am creating an upload/download function for an extranet site. Files will be uploaded to directory based upon the users login and associated project. The function works as long as I use "c:\Temp" as the directory. When I use any other hard coded directory or even Server.MapPath() the upload function fails and returns the error: "Exception has been thrown by the target of an invocation." Once I change the root directory to "c:\Temp",...
4
5901
by: Jim Michaels | last post by:
after a file upload, $_FILES is not populated but $_POST is. what's going on here? $_POST=C $_POST=C $_POST=C $_POST=C:\\www\\jimm\\images\\bg1.jpg $_FILES= $_FILES= $_FILES=
1
4870
by: gryffin | last post by:
im trying to do file extension checking but its not working :( i have the following in the head <script language="JavaScript"> extArray = new Array(".jpg", ".png", ".bmp"); function LimitAttach(form, file) { allowSubmit = false; if (!file) return; while (file.indexOf("\\") != -1)
2
1372
sun1programmer
by: sun1programmer | last post by:
Hi All, I have an HTML File upload control and I want to check the size of uploaded file by the user at the time of uploading. If size exceeds 25MB, then it shows the confirmation message that "File size exceeds 25MB. Do you want to continue anyway". If user clicks on OK button then the file is uploaded else not. I found many solution but all have some limitations. Please try your codes before posting. Thanks & Best of luck.
2
7653
by: hotflash | last post by:
Hi All, I found the best pure ASP code to upload a file to either server and/or MS Access Database. It works fine for me however, there is one thing that I don't like and have tried to fix but don't have any luck is to do a form validation. This script requires the files: db-file-to-disk.asp and _upload.asp. There is a DESCRIPTION field in the db-file-to-disk.asp file, what I want to do is the user has to field out this fied before...
1
5461
by: chrisj | last post by:
I'm using freeASPupload and got some assistance integrating to a Member script. It works successfully. In this modified version there are two groups that use this upload script. Members of one group get automatically re-directed after uploading. However, this member group never gets the benefit of knowing if they've uploaded an incorrect file size or incorrect file extension. Members from the second group do see the "exceeds max file...
6
3839
Jacotheron
by: Jacotheron | last post by:
I need a PHP script that can upload music files (mp3). The script is for a home project I have started a while ago. I have a MySQL database of all the music that I have. Other computers on the network should be able to connect to the database and run queries on the database or upload new music that does not yet exist on the database. The uploaded file's name should be in the following format: ARTIST - TITLE.mp3. I have the code to upload images,...
0
9685
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
9535
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10467
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...
1
10201
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10021
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
7558
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...
1
4130
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
3744
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2931
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.