---
layout: page
title: "JavaScript fseek function"
comments: true
sharing: true
footer: true
alias:
- /functions/view/fseek:805
- /functions/view/fseek
- /functions/view/805
- /functions/fseek:805
- /functions/805
---
<!-- Generated by Rakefile:build -->
A JavaScript equivalent of PHP's fseek

{% codeblock filesystem/fseek.js lang:js https://raw.github.com/kvz/phpjs/master/functions/filesystem/fseek.js raw on github %}
function fseek (handle, offset, whence) {
  // http://kevin.vanzonneveld.net
  // +   original by: Brett Zamir (http://brett-zamir.me)
  // *     example 1: var h = fopen('http://kevin.vanzonneveld.net/pj_test_supportfile_1.htm', 'r');
  // *     example 1: fseek(h, 100);
  // *     returns 1: 0

  var getFuncName = function (fn) {
    var name = (/\W*function\s+([\w\$]+)\s*\(/).exec(fn);
    if (!name) {
      return '(Anonymous)';
    }
    return name[1];
  };
  if (!this.php_js || !this.php_js.resourceData || !this.php_js.resourceDataPointer || !handle || !handle.constructor || getFuncName(handle.constructor) !== 'PHPJS_Resource') {
    return -1;
  }

  switch (whence) {
  case undefined:
    // fall-through
  case 'SEEK_SET':
    this.php_js.resourceDataPointer[handle.id] = offset / 2 + 1;
    break;
  case 'SEEK_CUR':
    this.php_js.resourceDataPointer[handle.id] += offset / 2 + 1;
    break;
  case 'SEEK_END':
    this.php_js.resourceDataPointer[handle.id] = this.php_js.resourceData[handle.id].length + offset / 2 + 1;
    break;
  default:
    throw 'Unrecognized whence value for fseek()';
  }
  return 0;
}
{% endcodeblock %}

 - [view on github](https://github.com/kvz/phpjs/blob/master/functions/filesystem/fseek.js)
 - [edit on github](https://github.com/kvz/phpjs/edit/master/functions/filesystem/fseek.js)

### Example 1
This code
{% codeblock lang:js example %}
var h = fopen('http://kevin.vanzonneveld.net/pj_test_supportfile_1.htm', 'r');
fseek(h, 100);
{% endcodeblock %}

Should return
{% codeblock lang:js returns %}
0
{% endcodeblock %}


### Other PHP functions in the filesystem extension
{% render_partial _includes/custom/filesystem.html %}
