ftruncate

  • We use the ftruncate function in PHP to truncate a file to a given length.

    PHP

    $f = fopen('/path/to/foobar', 'w')
    ftruncate($f, 10);
    fclose($f);

    The same task can be accomplished in Ruby with the File#truncate method:

    Ruby

    File.open('/path/to/foobar', 'w') do |f|
      f.truncate(10)
    end

    Ruby also provides the File.truncate class method for convenience. It allows you to truncate a file without opening it yourself.

    Ruby

    File.truncate('/path/to/foobar', 10)

    see also

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.