Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Detect file size

Posted on 2006-03-22
15
Medium Priority
?
339 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.
0
Comment
Question by:mathewparet
  • 4
  • 3
  • 3
  • +2
13 Comments
 
LVL 35

Expert Comment

by:TimYates
ID: 16256064
I don't believe you can.  But I'm interested to see if I'm wrong ;-)

*crosses fingers*

Tim
0
 
LVL 32

Expert Comment

by:jhance
ID: 16256135
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
 
LVL 12

Expert Comment

by:TheMegaLoser
ID: 16256194
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:mathewparet
ID: 16256224
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
 
LVL 35

Expert Comment

by:TimYates
ID: 16256291
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
 

Author Comment

by:mathewparet
ID: 16256333
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
 
LVL 35

Expert Comment

by:TimYates
ID: 16256366
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
 
LVL 12

Accepted Solution

by:
TheMegaLoser earned 600 total points
ID: 16256722
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
 

Author Comment

by:mathewparet
ID: 16256769
but how do I know the total file size before upload? That is how do I get the incomming content length in php?
0
 
LVL 12

Expert Comment

by:TheMegaLoser
ID: 16256968
I'm sorry to say that I'm not sure how to do that in php.
0
 
LVL 1

Expert Comment

by:pzmatek
ID: 16262155
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
 

Author Comment

by:mathewparet
ID: 16265915
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
 
LVL 1

Expert Comment

by:pzmatek
ID: 16266400
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

Featured Post

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Make the most of your online learning experience.
The SignAloud Glove is capable of translating American Sign Language signs into text and audio.
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…
Screencast - Getting to Know the Pipeline

581 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question