Moving a tmpfile()?

php.internals

Dan Liebner

3 years ago
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. Thanks, Dan

Robert Landers

3 years ago
On Sat, Apr 29, 2023 at 9:33 AM Dan Liebner <dliebner@gmail.com> 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. > > Thanks, > Dan
I suspect it depends on the OS. For example, in Linux, you can delete a file you are writing to without issues, but you cannot in Windows.

Hans Henrik Bergan

3 years ago
@Robert I know windows has problems with moving files that are opened by other processes, BUT this still works fine on Windows 10 running on NTFS: <?php $h = tmpfile(); $path = stream_get_meta_data($h)['uri']; var_dump(rename($path, __DIR__.'/test.f')); ?> rename returns true and the file really is moved (and the file is no longer automatically deleted - i suspect PHP's tmpfile() try to delete the original path on cleanup, and don't keep track of renames/movings) On Sat, 29 Apr 2023 at 14:56, Robert Landers <landers.robert@gmail.com> wrote:

Unnamed Person

3 years ago
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

Dan Liebner

3 years ago
> 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:

Unnamed Person

3 years ago
On Sunday 07 May 2023 02:30:13 (+02:00), Dan Liebner wrote:
> > 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?
Then why create a temporary file first? ^^ Nevertheless, a fair point it seems, and then thinking that this is what often happens with tmpfile() on a Linux system: - sys_get_temp_dir() is in the global file-system hierarchy and on a file-system of its own (tmpfs) - a move (rename) operation across file system boundaries is a copy & delete operation Given we're discussing file-system I/O to some extend here, and you're asking what could possibly go wrong: A lot when we don't expect it and then race conditions. Often we don't and even can't know in advance. Therefore, if there is a need to create a temporary file in the first place, do it. If not, don't create it, as otherwise you have the copy already and this prevents you from learning if the file transaction really needs it to become robust or it is just extra I/O for nothing (and perhaps introduces places to error you actually don't want to deal with). YMMV. -- hakre