tmpfilepath; tweaks to about
This commit is contained in:
@@ -54,7 +54,7 @@ And reference a fact in my task...
|
||||
### Summary
|
||||
|
||||
So now it's much easier to reference environment information from property files (via interpolation), make targets more
|
||||
conditional, and, of course, from actual tasks.
|
||||
conditional, and, of course, within actual tasks.
|
||||
|
||||
|
||||
[1]: https://puppetlabs.com/puppet/what-is-puppet/
|
||||
|
||||
30
blog/_posts/2013-03-05-path-based-tmpfile-in-php.md
Normal file
30
blog/_posts/2013-03-05-path-based-tmpfile-in-php.md
Normal file
@@ -0,0 +1,30 @@
|
||||
---
|
||||
title: Path-based tmpfile in PHP
|
||||
layout: post
|
||||
tags: php
|
||||
description: When paths are more useful than resources.
|
||||
---
|
||||
|
||||
PHP has the [`tmpfile`][1] function for creating a file handle which will automatically be destroyed when it is closed
|
||||
or when the script ends. PHP also has the [`tempnam`][2] function which takes care of creating the file and returning
|
||||
the path, but doesn't automatically destroy the file.
|
||||
|
||||
To get the best of both worlds (temp file + auto-destroy), I have found this useful:
|
||||
|
||||
{% highlight php startinline %}
|
||||
function tmpfilepath() {
|
||||
$path = stream_get_meta_data(tmpfile())['uri'];
|
||||
|
||||
register_shutdown_function(
|
||||
function () use ($path) {
|
||||
unlink($path);
|
||||
}
|
||||
);
|
||||
|
||||
return $path;
|
||||
}
|
||||
{% endhighlight %}
|
||||
|
||||
|
||||
[1]: http://php.net/manual/en/function.tmpfile.php
|
||||
[2]: http://php.net/manual/en/function.tempnam.php
|
||||
Reference in New Issue
Block a user