tmpfile
-
PHP’s
tmpfilefunction returns a resource to a temporarily file, opened with modew+.PHP
$f = tmpfile(); fwrite($f, 'foo'); rewind($f); var_export(fread($f)); //=> 'foo' fclose($f);
Ruby’s
Tempfileclass is a special kind ofFilethat accomplishes the same task.Unlike PHP’s
tmpfilewhich names the file without any parameters, you must specify a prefix thatTempfilewill use to build the filename.Ruby
require 'tempfile' Tempfile.open('prefix') do |f| f.write('foo') f.rewind p f.read #=> "foo" f.unlink end
Like any other
Filein Ruby, it’s a best practice in Rails applications to always openTempfileinstances with a block as shown above. For more on this, please see fopen.In PHP, the file is automatically unlinked when closed or at the end of the request. In Ruby, this is not the case, even within the context of the Rails framework.
Instead,
Tempfiledefines a finalizer which unlinks the file. However, the finalizer will not be called until the object is deferenced and collected by Ruby’s garbage collector. Since the garbage collection interval is indeterminate, we recommend that you always explicitlyunlinkanyTempfilewhen you are done with it.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.

