php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Doc Bug #44222 tempnam $prefix parameter limitation
Submitted: 2008-02-22 21:41 UTC Modified: 2008-11-07 11:35 UTC
Votes:2
Avg. Score:3.0 ± 0.0
Reproduced:1 of 2 (50.0%)
Same Version:1 (100.0%)
Same OS:1 (100.0%)
From: c dot d dot brengel at att dot net Assigned: pajoye (profile)
Status: Closed Package: Documentation problem
PHP Version: 5.2.5 OS: Windows XP
Private report: No CVE-ID: None
 [2008-02-22 21:41 UTC] c dot d dot brengel at att dot net
Description:
------------
It appears that when using "tempnam" the $prefix parameter only accepts 3 characters.  I used tempnam(getcwd(),'ADR'.date(Y.m.d).'_')
and the resulting file name ended up as "ADRBA.tmp" not the 
"ADR2008222_BA.tmp" that it should have given.

I then tried tempnam(getcwd(), 'ADR123') and stil got only ADRXX.tmp.

Not sure but would appreciate it if you could determine if this is in fact correct behavior.



Reproduce code:
---------------
<?php
echo getcwd() . "\n\r";
chdir('tmp');
echo getcwd() . "\n\r";
$file=tempnam(getcwd(),'ADR'.date(Y.m.d).'_');
echo "$file";
?>

Expected result:
----------------
C:\Inetpub\wwwroot\ADRAL 
C:\Inetpub\wwwroot\ADRAL\tmp 
C:\WINDOWS\Temp\ADR2008222_C2.tmp 


Actual result:
--------------
C:\Inetpub\wwwroot\ADRAL 
C:\Inetpub\wwwroot\ADRAL\tmp 
C:\WINDOWS\Temp\ADRC2.tmp 


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2008-03-06 23:08 UTC] a dot u dot savchuk at gmail dot com
As i understand it is not bug but described behavior of windows GetTempFileName() function.
So it is not possible to fix that (in easy manner).

From MSDN http://msdn2.microsoft.com/en-us/library/aa364991.aspx :
...
lpPrefixString
    The null-terminated prefix string. The function uses up to the first three characters of this string as the prefix of the file name.
...

Other possible solution is to create own version of GetTempFileName() under Windows
but i think  htta more simple and correct solution is to describe it in docu as Windows-specific issue...

Also there are some possible problems can occur with this function under Windows:
1. if temp directory path length more than MAXPATH-14 :
...
lpPathName
    The directory path for the file name. Applications typically specify a period (.) for the current directory or the result of the GetTempPath function. The string cannot be longer than MAX_PATH?14 characters or GetTempFileName will fail. If this parameter is NULL, the function fails.
...

so if templ directory will have (MAXPATHLEN-13) symbols
then PHP check of length will successfully pass
but GetTempFileName() will fail.

There is patch for fix this problem:

$ diff -upd /home/sawoy/source/php-5.2.5/main/php_open_temporary_file.c ./main/php_open_temporary_file.c
--- /home/sawoy/source/php-5.2.5/main/php_open_temporary_file.c 2007-08-10 03:13:15.000000000 -0700
+++ ./main/php_open_temporary_file.c    2008-03-06 15:00:22.000000000 -0800
@@ -131,7 +131,11 @@ static int php_do_open_temporary_file(co
                trailing_slash = "/";
        }

+#ifdef PHP_WIN32
+       if (spprintf(&opened_path, 0, "%s", new_state.cwd) > (MAXPATHLEN - 14)) {
+#else
        if (spprintf(&opened_path, 0, "%s%s%sXXXXXX", new_state.cwd, trailing_slash, pfx) >= MAXPATHLEN) {
+#endif
                efree(opened_path);
                free(new_state.cwd);
                return -1;


2. due algo of GetTempFileName() it will be not possible to create more than 65535 temporary files in on temp directory. 
On box where i tested it it can create only 65324 files and on next script hangs...

Also for info: time of creation 65xxx-th file in one temp directory is encreased in ~10-15 times :)
 [2008-03-07 00:12 UTC] c dot d dot brengel at att dot net
Thank You.  I just needed to know if was a problem with my system, PHP or Windows.  Thank You for confirming it is a Windows problem.

I have used another work around so I do not need to use "tempnam".

It would be a good idea to update the PHP documentation to let others know of this behavior on WIndows.  

Keep up the good work
 [2008-11-07 11:35 UTC] vrana@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.

"Windows use only the first three characters of prefix."
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu May 09 16:01:31 2024 UTC