On Saturday 29 April 2023 09:32:42 (+02:00), Dan Liebner wrote:
> Are there any inherent problems with moving a file created with
tmpfile()?
>
> In practice, it seems that it can be done and the file will not be
deleted
> after being moved and the file handle closed.
yes, not that it would be inherently wrong to do it that way, it is that
tmpfile() already offers the file handle, so you can rewind() and put the
contents in your destination file:
$destinationPath = tempnam(__DIR__, '.destination.');
$tempHandle = tmpfile();
# ... operate on $tempHandle ...
fwrite($tempHandle, "hello world\n");
rewind($tempHandle);
$result = file_put_contents($destinationPath, $tempHandle);
fclose($tempHandle);
Why move the temporary file when it is already a temporary file, right?
-- hakre
--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: https://www.php.net/unsub.php