Re: Moving a tmpfile()?
| From: | Dan Liebner | Date: | Sun, 07 May 2023 00:30:13 +0000 |
| Subject: | Re: Moving a tmpfile()? | ||
| References: | 1 2 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-120207@lists.php.net to get a copy of this message | ||
> Why move the temporary file when it is already a temporary file, right?
If you don't want to have to write the file again with a copy?
On Sat, May 6, 2023 at 1:56 PM Hans Krentel <hanskrentel@yahoo.de> wrote:
>
>
>
> 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
>