# Moving a tmpfile()?

- Thread: https://externals.io/message/120158
- Original archive: https://news-web.php.net/php.internals/120158
- Started by Dan Liebner on 2023-04-29T07:32:42+00:00
- Messages: 5
- Reply by email to internals@lists.php.net
- More for agents: search with https://externals.io/search.md?q={query} · JSON API at https://externals.io/api · documentation at https://externals.io/llms.txt

---

## #120158 — Dan Liebner — 2023-04-29T07:32:42+00:00

URL: https://externals.io/message/120158#120158

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

---

## #120162 — Robert Landers — 2023-04-29T12:56:13+00:00

URL: https://externals.io/message/120158#120162 · In reply to: #120158

>
> 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.

---

## #120164 — Hans Henrik Bergan — 2023-04-30T07:55:58+00:00

URL: https://externals.io/message/120158#120164 · In reply to: #120162

@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)



>
> 
> > […]
>
> 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.
>
> --
> 
> To unsubscribe, visit: https://www.php.net/unsub.php

---

## #120206 — Hans Krentel via internals — 2023-05-06T17:56:35+00:00

URL: https://externals.io/message/120158#120206 · In reply to: #120158

> 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

---

## #120207 — Dan Liebner — 2023-05-07T00:30:13+00:00

URL: https://externals.io/message/120158#120207 · In reply to: #120206

> 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?



>
>
>
> 
>
> > […]
> `tmpfile()`?
> > […]
> deleted
> > […]
>
> 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
