Use PHP functions in JavaScript

JavaScript filesize

Get file size

1
2
3
4
56
7
8
9
1011
12
13
14
1516
17
18
19
2021
22
23
24
2526
27
28
29
3031
32
33
34
35
function filesize (url) {
    // Get file size  
    // 
    // version: 909.322
    // discuss at: http://phpjs.org/functions/filesize    // +   original by: Enrique Gonzalez
    // +      input by: Jani Hartikainen
    // +   improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   improved by: T. Wild
    // %        note 1: This function uses XmlHttpRequest and cannot retrieve resource from different domain.    // %        note 1: Synchronous so may lock up browser, mainly here for study purposes. 
    // *     example 1: filesize('http://kevin.vanzonneveld.net/pj_test_supportfile_1.htm');
    // *     returns 1: '3'
    var req = this.window.ActiveXObject ? new ActiveXObject("Microsoft.XMLHTTP") : new XMLHttpRequest();
    if (!req) {throw new Error('XMLHttpRequest not supported');}    
    req.open('HEAD', url, false);
    req.send(null);
    
    if (!req.getResponseHeader) {        try {
            throw new Error('No getResponseHeader!');
        } catch (e){
            return false;
        }    } else if (!req.getResponseHeader('Content-Length')) {
        try {
            throw new Error('No Content-Length!');
        } catch (e2){
            return false;        }
    } else {
        return req.getResponseHeader('Content-Length'); 
    }
}
external links: original PHP docs | raw js source

Examples

Running

1
filesize('http://kevin.vanzonneveld.net/pj_test_supportfile_1.htm');

Should return

1
'3'

Dependencies

No dependencies, you can use this function standalone.

Open syntax issues

php.js uses JsLint to help us keep our code consistent and prevent some common bugs.

Eventually we want all code to pass or at least take into consideration most fixes suggested by JsLint, following this JsLint configuration we’ve decided on.


Authors

Thanks to the following developers, you get to have filesize goodness in JavaScript.

Comments

Add Comment
Use:
[CODE]
your_stuff('here');
[/CODE]
for proper code formatting
By submitting code here you are allowing us to use it in php.js hence dual licensing it under the MIT and GPL licenses

Gravatar
Kevin van Zonneveld
10 Dec '08 Permalink

q  @ Arcanis & T. Wild: Thanks for your input guys. T.Wild, I've implemented your suggestion and am curious what people will say about this approach. If the responses are good we might implement it in other cases as well.

Gravatar
T.Wild
8 Dec '08 Permalink

q   @Arcanis
While it may be stupid not to throw an error when the content length header isn't sent; when JavaScript throws an error the script halts, just try this:

1
2
throw new Error('error1');
alert("FOO"); //This is not called

You don't want your entire script to halt simply because the server fails to send the correct headers which isn't a client side error.
However the XHR is different it is an error on the client side and does warrant an error, but even then I'd rather it return false rather than throw an error since PHP returns false on any error, not halt your script.
Maybe this would work?
[CODE type="Javascript"]
//This allows the error to be thrown while still returning false;
try{
throw new Error('error1');
}catch(e){
return false;
}

//In this case the error is still thrown AND the alert is called;
try{
throw new Error('error1');
}catch(e){};
alert("FOO");
[/CODE]
I know I may not be clear on what I'm saying but I hope you see what I'm getting at.

Gravatar
Arcanis
7 Dec '08 Permalink

q  I think it's stupid to return false when there is no content-length, and to return an error when there is no XHR.
Both of them are errors, no ? I think you must send an error if the content-length is not specified.

Gravatar
Kevin van Zonneveld
18 Nov '08 Permalink

q  Jani Hartikainen: Fair enough!

Gravatar
Jani Hartikainen
17 Nov '08 Permalink

q  While the whole method is a bit dubious (as pointed out by Onno Marsman), it might be a general improvement to check for window.XMLHttpRequest, rather than relying on error catching. To my understanding, catching exceptions (errors?) is slow.

Gravatar
Kevin van Zonneveld
14 Nov '08 Permalink

q  @ Onno Marsman: Haha, that's ok Onno! appreciated. And about the compiler... Man I'm so busy :| Will try to dedicate more time to it.

Gravatar
Onno Marsman
14 Nov '08 Permalink

q  K thnx, I will not bother you with it again ;)
I hope that compiler will soon be available.

Gravatar
Kevin van Zonneveld
14 Nov '08 Permalink

q  @ Onno Marsman: I've made it return false if it doesn't. We've had this discussion before indeed. I know how you feel about this, and you know I agree but these functions serve a purpose of their own. We'll make sure they'll not be included by default with our future compiler. Thanks for your input Onno.

Gravatar
Onno Marsman
14 Nov '08 Permalink

q  What if the "Content-Length" header does not exist?

Furthermore: it's really weird to use these types of functions. Making an Ajax request which executes the filesize function server-side would be many times better. But the same goes for something like file_get_contents and this has pointed out before. I really think that in the future these types of functions (along with functions like sleep, which also nobody should use) should not remain in the main download of php.js, but optionally available.


Contribute a New function

More functions

In this category

basename
dirname
fclose
feof
fgetc
fgetcsv
fgets
fgetss
file
file_exists
file_get_contents
filemtime
» filesize
fopen
fpassthru
fread
fseek
ftell
pathinfo
pclose
popen
readfile
realpath
rewind

Support us

spread the word:


Use any PHP function in JavaScript


These kind folks have already donated: Anonymous and Shawn Houser.
<your name here>

Click here to lend your support to: phpjs and make a donation at www.pledgie.com !