JavaScript fopen
Open a file or a URL and return a file pointer
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 3536 37 38 39 4041 42 43 44 4546 47 48 49 5051 52 53 54 5556 57 58 59 6061 62 63 64 6566 67 68 69 7071 72 73 74 7576 77 78 79 8081 82 83 84 8586 87 88 89 9091 92 93 94 95 | function fopen (filename, mode, use_include_path, context) { // Open a file or a URL and return a file pointer // // version: 1008.1718 // discuss at: http://phpjs.org/functions/fopen // + original by: Brett Zamir (http://brett-zamir.me) // + input by: Paul Smith // + bugfixed by: Brett Zamir (http://brett-zamir.me) // - depends on: file_get_contents // * example 1: fopen('http://kevin.vanzonneveld.net/pj_test_supportfile_1.htm', 'r'); // * returns 1: 'Resource id #1' var resource={}, i=0, that = this; var getFuncName = function (fn) { var name = (/\W*function\s+([\w\$]+)\s*\(/).exec(fn); if (!name) { return '(Anonymous)'; } return name[1]; }; // BEGIN file inclusion: file_get_contents var file_get_contents = function ( url ) { var req = that.window.ActiveXObject ? new ActiveXObject("Microsoft.XMLHTTP") : new XMLHttpRequest(); if (!req) { throw new Error('XMLHttpRequest not supported'); } if (!(/^http/).test(url)) { // Allow references within or below the same directory (should fix to allow other relative references or root reference; could make dependent on parse_url()) url = that.window.location.href + '/' +url; } req.open("GET", url, false); req.send(null); return req.responseText; }; // END file inclusion if (use_include_path === 1 || use_include_path === '1' || use_include_path === true) { // Not implemented yet: Search for file in include path too } if (context) { // Not implemented yet, but could be useful to modify nature of HTTP request, etc. } for (i=0; i < mode.length; i++) { // Have to deal with other flags if ever allow if (mode.charAt(i) === 'r' && (!mode.charAt(i+1) || mode.charAt(i+1) !== '+')) { continue; } switch (mode.charAt(i)) { case 'r': // must have '+' now case 'w': // or 'w+' case 'a': // or 'a+' case 'x':// or 'x+' throw 'Writing is not implemented'; case 'b': case 't': throw 'Windows-only modes are not supported'; default: throw 'Unrecognized file mode passed to '+getFuncName(arguments.caller)+'()'; } } // BEGIN REDUNDANT this.php_js = this.php_js || {}; this.php_js.resourceData = this.php_js.resourceData || {}; this.php_js.resourceDataPointer = this.php_js.resourceDataPointer || {}; this.php_js.resourceIdCounter = this.php_js.resourceIdCounter || 0; // END REDUNDANT // BEGIN STATIC function PHPJS_Resource (type, id, opener) { // Can reuse the following for other resources, just changing the instantiation // See http://php.net/manual/en/resource.php for types this.type = type; this.id = id; this.opener = opener; } PHPJS_Resource.prototype.toString = function () { return 'Resource id #'+this.id; }; PHPJS_Resource.prototype.get_resource_type = function () { return this.type; }; PHPJS_Resource.prototype.var_dump = function () { return 'resource('+this.id+') of type ('+this.type+')'; }; // END STATIC this.php_js.resourceIdCounter++; this.php_js.resourceData[this.php_js.resourceIdCounter] = this.file_get_contents(filename); this.php_js.resourceDataPointer[this.php_js.resourceIdCounter] = 0; resource = new PHPJS_Resource('stream', this.php_js.resourceIdCounter, 'fopen'); resource.mode = mode; // Add file-specific attributes return resource; // may be 'file' instead of 'stream' type on some systems } |
Examples
Running
1 | fopen('http://kevin.vanzonneveld.net/pj_test_supportfile_1.htm', 'r'); |
Should return
1 | 'Resource id #1' |
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 fopen goodness in JavaScript.

when i used your code I had an error ;
Kevin van Zonneveld
May 14th