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.
mathewparetAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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

*crosses fingers*

Tim
0
jhanceCommented:
Fix your script so that it sends the file size before starting the upload.  Then your server will know how much data to expect.
0
TheMegaLoserCommented:
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!
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

mathewparetAuthor 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!
0
TimYatesCommented:
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
0
mathewparetAuthor 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
0
TimYatesCommented:
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
0
TheMegaLoserCommented:
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.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
mathewparetAuthor Commented:
but how do I know the total file size before upload? That is how do I get the incomming content length in php?
0
TheMegaLoserCommented:
I'm sorry to say that I'm not sure how to do that in php.
0
pzmatekCommented:
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'];
0
mathewparetAuthor 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.
0
pzmatekCommented:
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?
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Programming

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.