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
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.
@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)
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,
DanI 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
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
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?
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 canrewind()
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