php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #72317 tempnam fails with prefix "php"
Submitted: 2016-06-02 15:35 UTC Modified: 2016-06-06 11:36 UTC
From: flip101 at gmail dot com Assigned: cmb (profile)
Status: Not a bug Package: *Directory/Filesystem functions
PHP Version: 5.6.20 OS: Windows 10 64 bits
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:
23 - 5 = ?
Subscribe to this entry?

 
 [2016-06-02 15:35 UTC] flip101 at gmail dot com
Description:
------------
Not sure if the "php" prefix is reserved, but if it is, it's not documented at https://secure.php.net/manual/en/function.tempnam.php

Note: that i made up the expected results (on line 4), not sure how the random filename algorithm works.

Test script:
---------------
var_dump(
  file_exists(sys_get_temp_dir()),
  is_writable(sys_get_temp_dir()),
  sys_get_temp_dir(),
  tempnam(sys_get_temp_dir(), 'php'),
  tempnam(sys_get_temp_dir(), 'aaa')
);

Expected result:
----------------
bool(true)
bool(true)
string(34) "C:\Users\flip101\AppData\Local\Temp"
string(45) "C:\Users\flip101\AppData\Local\Temp\php2EC.tmp"
string(45) "C:\Users\flip101\AppData\Local\Temp\aaaF2A.tmp"

Actual result:
--------------
bool(true)
bool(true)
string(34) "C:\Users\flip101\AppData\Local\Temp"
bool(false)
string(45) "C:\Users\flip101\AppData\Local\Temp\aaaF2A.tmp"

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2016-06-03 12:21 UTC] cmb@php.net
-Status: Open +Status: Feedback
 [2016-06-03 12:21 UTC] cmb@php.net
I can't reproduce this issue, neither on Windows 10 x64 with
PHP 5.6.22 VC11 x64 Non Thread Safe (2016-May-26 18:22:21), nor on
3v4l.org, see <https://3v4l.org/Wig5o>.

Please make sure that you have error_reporting=-E_ALL, and double-
check that there are no warnings or notices reported.
 [2016-06-03 13:20 UTC] flip101 at gmail dot com
-Status: Feedback +Status: Open -PHP Version: 5.6.22 +PHP Version: 5.6.20
 [2016-06-03 13:20 UTC] flip101 at gmail dot com
I double check error reporting it's on E_ALL both in my php.ini (and i double check that i was viewing the right php.ini by checking which one was loaded) and also i put error_reporting(E_ALL); at the top (new script at the bottom of this comment). I don't see any warnings or notices, exact output at bottom of comment. My exact php version is this one: php-5.6.20-Win32-VC11-x64.zip

==== Script
<?php
error_reporting(E_ALL);
var_dump(
  file_exists(sys_get_temp_dir()),
  is_writable(sys_get_temp_dir()),
  sys_get_temp_dir(),
  tempnam(sys_get_temp_dir(), 'php'),
  tempnam(sys_get_temp_dir(), 'aaa')
);
==== End script

==== Console
C:\dev>php test.php
bool(true)
bool(true)
string(34) "C:\Users\plagas\AppData\Local\Temp"
bool(false)
string(46) "C:\Users\plagas\AppData\Local\Temp\aaaE26E.tmp"

==== End Console
 [2016-06-03 13:57 UTC] flip101 at gmail dot com
Forgot to change the username in my output, but it's the same user ..
 [2016-06-03 15:12 UTC] cmb@php.net
> My exact php version is this one: php-5.6.20-Win32-VC11-x64.zip

Running your latest test script on this very version:

    bool(true)
    bool(true)
    string(31) "C:\Users\cmb\AppData\Local\Temp"
    string(43) "C:\Users\cmb\AppData\Local\Temp\phpB8D1.tmp"
    string(43) "C:\Users\cmb\AppData\Local\Temp\aaaB8E2.tmp"

So, I still can't reproduce the issue. Anyhow, note[1]:

| The x64 builds of PHP 5 for Windows are experimental,

I suggest you try with an x86 build of PHP 5.6.22.

[1] <http://windows.php.net/download/>
 [2016-06-06 07:44 UTC] ab@php.net
-Status: Open +Status: Feedback
 [2016-06-06 07:44 UTC] ab@php.net
Thanks for the report. Can't reproduce as well. Also it's unlikely an issue with exactly x64 PHP5, as the I/O layer is same. 

@flip101 does the same happen when using a dir different from temp? For one, could you please post the relevant part of the procmon output when running into this issue? The Process Monitor is fetcheable from sysinternals.com.

Thanks.
 [2016-06-06 11:25 UTC] flip101 at gmail dot com
Hi ab, i captured the output of Sysinternals Process Monitor. I noticed that when trying to generate a file the file already exist (i went into the directory and indeed the file is there). I have 65.535 files which look like php####.tmp where #### can be 1 to 4 characters and seem to be counting up. However all these files were created on the day that i reported this issue. What i think happened is that i had the tempnamp() in a while loop (which had a wrong stop condition) and assumed php would delete the file if i don't use it (because i thought the file was temporary because of the function name). When i was getting errors there i started making the test case which of course failed as well.

Let me know if you want to have the output of Process Monitor, or @cmb if you want to test this with a different php version. But i think the issue is resolved at this point.
 [2016-06-06 11:36 UTC] cmb@php.net
-Status: Feedback +Status: Not a bug -Assigned To: +Assigned To: cmb
 [2016-06-06 11:36 UTC] cmb@php.net
> I have 65.535 files which look like php####.tmp where #### can
> be 1 to 4 characters and seem to be counting up. […] and assumed
> php would delete the file if i don't use it […]

Well, that is documented[1]:

| If PHP cannot create a file in the specified dir parameter, it
| falls back on the system default. On NTFS this also happens if
| the specified dir contains more than 65534 files.

In this case there's no fallback on the system default, because
that has already been explicitly requested.

And:

| Note, that you need to remove the file in case you need it no
| more, it is not done automatically.

> But i think the issue is resolved at this point.

ACK. Thanks for your quick feedback. :)

[1] <http://php.net/manual/en/function.tempnam.php>
 [2016-06-06 11:41 UTC] flip101 at gmail dot com
Agreed cmb, though note that there didn't seem to be a limit 65534 files because there were more than 80.000 files and php was still able to create files with prefix "aaa" (because it didn't exhaust the possible combinations for this prefix).
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri Mar 29 09:01:28 2024 UTC