Use PHP functions in JavaScript

JavaScript md5_file

Calculate the md5 hash of given filename

1
2
3
4
56
7
8
9
1011
12
13
14
1516
17
18
19
2021
22
function md5_file ( str_filename ) {
    // Calculate the md5 hash of given filename  
    // 
    // version: 909.322
    // discuss at: http://phpjs.org/functions/md5_file    // +   original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +      input by: Brett Zamir (http://brett-zamir.me)
    // +   bugfixed by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // -    depends on: file_get_contents
    // -    depends on: md5    // *     example 1: md5_file('http://kevin.vanzonneveld.net/pj_test_supportfile_1.htm');
    // *     returns 1: '202cb962ac59075b964b07152d234b70'
    var buf = '';
 
    buf = this.file_get_contents(str_filename);    
    if (!buf) {
        return false;
    }
        return this.md5(buf);
}
external links: original PHP docs | raw js source

Examples

Running

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

Should return

1
'202cb962ac59075b964b07152d234b70'

Dependencies

In order to use this function, you also need:

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 md5_file 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
Brett Zamir
Jan 7th Permalink

q  @smileart (and @Robert too, for that matter): Sorry, but I'm not sure the technique used in file_get_contents() to get binary data works perfectly (and probably not in IE anyways--see the notes below), or perhaps the way we're using it. If someone has time, it would be useful to know whether the issue lies in our md5() implementation or in the file_get_contents(). If the problem is the latter, it would be nice to have a very short file (as short as possible) that demonstrates the issue. If the problem is the former, we should be discussing this on the md5() page.

Gravatar
smileart
Jan 6th Permalink

q  Sorry, but it's return different result than PHP md5() function. Is there any way to fix it?

Gravatar
Robert
18 Dec '09 Permalink

q  It is also returning a different value every time it runs.

Gravatar
Robert
18 Dec '09 Permalink

q  No joy on that still returns a different value.

Gravatar
Robert
18 Dec '09 Permalink

q  I will try you last suggestion and let you know.

Gravatar
Brett Zamir
16 Dec '09 Permalink

q  In doing testing, we should also confirm whether md5() itself is giving correct results for the file represented as a string in PHP and our version...

Gravatar
Brett Zamir
15 Dec '09 Permalink

q   Nope, not that either... This should be it sorry...

1
2
3
4
56
function md5_file ( str_filename ) {
    var buf = ''; 
    buf = this.file_get_contents(str_filename, 'FILE_BINARY');
    buf = buf.replace(/[\s\S]/g, function (n) {return String.fromCharCode(n.charCodeAt(0) & 0xFF);});
    return buf ? this.md5(buf) : false;}

Gravatar
Brett Zamir
15 Dec '09 Permalink

q   Argh, sorry, two mistakes in that one...Try this:

1
2
3
4
5
function md5_file ( str_filename ) {
    var buf = ''; 
    buf = this.file_get_contents(str_filename, 'FILE_BINARY');
    return buf ? this.md5(buf).replace(/[\s\S]/g, function (n) {return n.charCodeAt(0) & 0xFF;}) : false;
}

Gravatar
Brett Zamir
15 Dec '09 Permalink

q   @Robert: Could you check whether the following works? Make sure you have the latest version of file_get_contents() too. It might only work in Firefox (and not IE) though, since it relies on overrideMimeType()...

I think we need to get the file in binary form as PHP does (we currently don't by default because it is usually not as useful). But maybe we should though to reflect the PHP API...

Anyways, let us know if this approach works at least in FF... I think my regular expression is correct and necessary there to shift back everything from the private characters into the normal ASCII range...(the trick used inside file_get_contents() is documented at https://developer.mozilla.org/En/Using_XMLHttpRequest#Receiving_binary_data )

1
2
3
4
56
function md5_file ( str_filename ) {
    var buf = ''; 
    buf = this.file_get_contents(str_filename, 'FILE_BINARY');
    buf = buf.replace(/[\s\S]/g, function (n) {n.charCodeAt(0) & 0xFF;});
    return buf ? this.md5(buf) : false;}

Gravatar
Robert Zebedee
15 Dec '09 Permalink

q  To update my last comment it is not creating the md5 of the file name but it is not creting an md5 which matches the md5sum in bash or md5_file in perl.

Gravatar
Robert Zebedee
15 Dec '09 Permalink

q  I have run your scripts and I have found a flaw the md5 that the scripts produce is of the file name not the file itself. The md5_file function in perl is the md5 of the file not the file name.

Gravatar
php five
10 Apr '08 Permalink

q  your php.js script is Amazing

Gravatar
Kevin van Zonneveld
18 Feb '08 Permalink

q  @ cagri ekin: I will look into it & credit you accordingly if I decide to use the code. Thank you cagri!

Gravatar
cagri ekin
18 Feb '08 Permalink

q  you may want to check this link for parse_str,

http://www.phpbuilder.com/board/showthread.php?t=10349280


Contribute a New function