We help IT Professionals succeed at work.

Detect file size

mathewparet
mathewparet asked
on
Medium Priority
383 Views
Last Modified: 2007-11-27
Hi,

I am a PHP developer. I have done a script that uploads a user inputted file. The file upload is monitored using ajax. But unfortunately I can only detect the current upload size (the size uploaded yet) and not the actual file size. What I want is to detect the total file size before starting the upload process. It maybe in Javascript or PHP. Any help is appreciated. Thanks in advance.

--
Thanks & Regards,
Mathew Paret.
Comment
Watch Question

CERTIFIED EXPERT
Top Expert 2004

Commented:
I don't believe you can.  But I'm interested to see if I'm wrong ;-)

*crosses fingers*

Tim

Commented:
Fix your script so that it sends the file size before starting the upload.  Then your server will know how much data to expect.
CERTIFIED EXPERT

Commented:
jhance, I don't think you can send the file size before starting the upload. The reason is that you shouldn't be able to inspect the visitors hard drive for security reasons.

The only way to do this I believe is to run some kind of signed code, ie an activex control or java applet or perhaps a signed java script.

For more information on this, see for example:

http://www.mozilla.org/projects/security/components/signed-scripts.html

Hope it helps!

Author

Commented:
But there should be some way! Because.....there are some sites that display progress bars indicating the % uploaded. So there should be some way using Javascript. Also I want the script to run successfully in Internet Explorer as well as Mozilla FireFox. Thanks again!
CERTIFIED EXPERT
Top Expert 2004

Commented:
Either

1)  They upload progress is lying and it's an exponential progress meter (ie, slowly crawls towards 100%, but will never get there) MS Access used to have one of these for its datbase compaction dialog ;-)
2)  They use a signed ActiveX control or signed Applet like TheMegaLoser says

Tim

Author

Commented:
Here is a example I saw. It works without assumption and works properly.

http://pdoru.from.ro/upload-progress-meter/upload-progress-meter-v4/live-demo/index.php
CERTIFIED EXPERT
Top Expert 2004

Commented:
Not sure how it does it...  I guess it must go low level, and look at the socket information...

I'd use that one then in your project :-)

Tim
CERTIFIED EXPERT
Commented:
Ok, I can understand how this is done:

1) Assign a upload ID. As soon as the upload starts your script starts receiving data. Store the incoming content-length in a database or similar.

2) Create an upload meter and give it the upload id. Now you know the complete file size by looking up the save content length.

3) Update how much info you have received and reload the window in #2 every x seconds.

This way you don't actually have the information before the sending starts but you have the information you need to create a nice upload solution.

To be clear, this should be coded serverside.

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts

Author

Commented:
but how do I know the total file size before upload? That is how do I get the incomming content length in php?
CERTIFIED EXPERT

Commented:
I'm sorry to say that I'm not sure how to do that in php.

Commented:
Of course you can do it.
The file of the file being uploaded is in a special variable called $_FILES.
You refer to it this way:

The form:
<form enctype="multipart/form-data" action="_URL_" method="post">
 Send this file: <input name="userfile" type="file" />
 <input type="submit" value="Send File" />
</form>

The script reading the size:

      echo "file size is".$_FILES['userfile']['size'];
      echo "file name is".$_FILES['userfile']['name'];
      echo "file type is".$_FILES['userfile']['type'];

Author

Commented:
Yes this would work, BUT the size can be derived only after the upload is completed, in yhis case. I want to detect the file size before the upload starts.

Commented:
Maybe these will help or at least clear the subject:
http://www.motobit.com/util/upload/Upload-Images-FileSize-Unique-Notification.ASP
http://msdn.microsoft.com/msdnmag/issues/0500/web/
http://www.oop-reserch.com/mime_example_2.html

I don't know what size you expect but if you want to restrict the size then you could do a doublecheck.
Let the user to upload the file. Show him the size and tell it's too large (or cut the uploding process
if the file is far far too big). What do you need it for exactly?
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.