Link to home
Start Free TrialLog in
Avatar of mathewparet
mathewparet

asked on

Detect file size

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.
Avatar of TimYates
TimYates
Flag of United Kingdom of Great Britain and Northern Ireland image

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

*crosses fingers*

Tim
Avatar of jhance
jhance

Fix your script so that it sends the file size before starting the upload.  Then your server will know how much data to expect.
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!
Avatar of mathewparet

ASKER

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!
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
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
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
ASKER CERTIFIED SOLUTION
Avatar of TheMegaLoser
TheMegaLoser
Flag of Sweden image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
but how do I know the total file size before upload? That is how do I get the incomming content length in php?
I'm sorry to say that I'm not sure how to do that in php.
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'];
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.
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?