473,593 Members | 2,813 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 9261
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
3919
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
5447
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
2753
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
5888
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
4857
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
1367
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
7640
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
5442
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
3824
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
7936
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
8241
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
8366
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8227
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...
0
6646
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5402
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
3893
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2383
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
1
1469
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.