php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Doc Bug #67105 tmpfile() may not remove the temporary file
Submitted: 2014-04-22 00:54 UTC Modified: 2018-12-28 23:28 UTC
Votes:1
Avg. Score:3.0 ± 0.0
Reproduced:1 of 1 (100.0%)
Same Version:1 (100.0%)
Same OS:1 (100.0%)
From: nate at frickenate dot com Assigned: cmb (profile)
Status: Closed Package: Filesystem function related
PHP Version: Irrelevant OS: Ubuntu 14.04
Private report: No CVE-ID: None
View Add Comment Developer Edit
Anyone can comment on a bug. Have a simpler test case? Does it work for you on a different platform? Let us know!
Just going to say 'Me too!'? Don't clutter the database with that please !
Your email address:
MUST BE VALID
Solve the problem:
37 + 14 = ?
Subscribe to this entry?

 
 [2014-04-22 00:54 UTC] nate at frickenate dot com
Description:
------------
The documentation for the tmpfile() function is wrong and misleading:

"The file is automatically removed when closed (for example, by calling fclose(), or when there are no remaining references to the file handle returned by tmpfile()), or when the script ends.

For details, consult your system documentation on the tmpfile(3) function, as well as the stdio.h header file."

The fact is php does *not* use tmpfile(3) for its implementation. That C function *guarantees* that there will not be a temporary file left on disk once the program exists, as the file is unlinked even before tmpfile(3) even returns.

However, as php relies on its own internal implementation of temporary files instead of using tmpfile(3), not only is the file not unlinked before returning the handle, but zombie files on disk are possible. This is shown in the attached test script.

Please update the documentation to not only remove the misleading information about tmpfile(3), but to explicitly state and warn that the function does *not* use tmpfile(3). Php devs are used to a C-named function being C-equivalent, and it causes problems when this is not the case.

Test script:
---------------
<?php

// 1. run this script from command-line, do steps 2 and 3 while sleeping.
// 2. check temp directory. file exists, would not be the case with tmpfile(3).
// 3. ctrl+c quit the running script. temp file is zombied, not unlinked.

$fh = tmpfile();
sleep(500);

Expected result:
----------------
Technically, I would expect php's tmpfile() to use tmpfile(3). However, I have seen existing code using tmpfile() and then stream_get_meta_data() to extract the 'uri' argument to manipulate the file on disk (ex: using it with ZipArchive::open()). Since a proper tmpfile(3) implementation would result in no accessible file on disk, this type of code would break.

So instead update the documentation to reflect that tmpfile() does not use tmpfile(3) and that zombie temporary files are possible if php exits unexpectedly.


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2017-01-28 17:04 UTC] cmb@php.net
-Package: Documentation problem +Package: Filesystem function related
 [2018-12-28 23:22 UTC] cmb@php.net
-Summary: Fix documentation for tmpfile +Summary: tmpfile() may not remove the temporary file -Status: Open +Status: Verified -Assigned To: +Assigned To: cmb
 [2018-12-28 23:22 UTC] cmb@php.net
> The documentation for the tmpfile() function is wrong and misleading:

ACK.

> Php devs are used to a C-named function being C-equivalent, and
> it causes problems when this is not the case.

While some PHP functions are “inherited” from C, few are actually
simple wrappers; for instance, PHP's string functions are supposed
to be binary safe while C works with zero terminated strings.  You
should not rely on eponymous functions to behave exactly as
specified by POSIX or C.
 [2018-12-28 23:27 UTC] cmb@php.net
Automatic comment from SVN on behalf of cmb
Revision: http://svn.php.net/viewvc/?view=revision&amp;revision=346471
Log: Fix #67105: tmpfile() may not remove the temporary file
 [2018-12-28 23:28 UTC] cmb@php.net
-Status: Verified +Status: Closed
 [2018-12-28 23:28 UTC] cmb@php.net
This bug has been fixed in the documentation's XML sources. Since the
online and downloadable versions of the documentation need some time
to get updated, we would like to ask you to be a bit patient.

Thank you for the report, and for helping us make our documentation better.
 [2020-02-07 06:05 UTC] phpdocbot@php.net
Automatic comment on behalf of cmb
Revision: http://git.php.net/?p=doc/en.git;a=commit;h=5a226ca916dd7faf31cf0570f8975b81f240ba88
Log: Fix #67105: tmpfile() may not remove the temporary file
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri Mar 29 11:01:29 2024 UTC