ftell

  • We use PHP’s ftell function to return the current position of a file pointer.

    PHP

    $f = fopen('/path/to/foobar', 'r');
    fseek($f, 2);
    print ftell($f);
    //=> 2
    fclose($f);

    The File#tell instance method works exactly the same way.

    Ruby

    File.open('/path/to/foobar', 'r') do |f|
      f.seek 2
      puts f.tell
      #=> 2
    end

    Ruby IO objects also provide the aliases pos and pos= for working with the file pointer’s position. It’s a common Ruby idiom to use these instead of seek and tell, but you’ll find both are widely used.

    Ruby

    File.open('/path/to/foobar', 'r') do |f|
      f.pos = 2
      puts f.pos
      #=> 2
    end

    see also

fstat ftruncate

Looking for Rails or PHP web application development, integration, and training?

Rails for PHP is brought to you by Maintainable Software. Get custom web applications and personalized training from the authors of the book and website.