# [RFC] Support optional suffix parameter in tempnam

- Thread: https://externals.io/message/120893
- Original archive: https://news-web.php.net/php.internals/120893
- Started by Athos Ribeiro on 2023-08-08T19:11:01+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

---

## #120893 — Athos Ribeiro — 2023-08-08T19:11:01+00:00

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

Hi internals,

As a follow-up on my previous message here
(https://marc.info/?l=php-internals&m=168912164828942&w=2),
I would like to start the discussion on the "Support optional suffix
parameter in tempnam" RFC.
https://wiki.php.net/rfc/tempnam-suffix-v2

The goal is, as suggested by its title, to allow an optional parameter
to the tempnam function, which would set a suffix to the created
temporary file.

This is a take-over on a previous attempt by Stefan Neufeind
(https://wiki.php.net/rfc/tempnam-suffix), who I am Ccing for
visibility.

I left some of my concerns regarding the proposed implementation in the
open issues section along with arguments for leaving them as is.
Provided no suggestions/discussions arrise from them, I will move them
to the future scope section before starting the voting phase for this
change.

Thanks in advance for any feedback here.

regards,

-- 
Athos Ribeiro

---

## #120897 — come@chilliet.eu — 2023-08-14T15:57:53+00:00

URL: https://externals.io/message/120893#120897 · In reply to: #120893

Le mardi 8 août 2023, 21:11:01 CEST Athos Ribeiro a écrit :
> Hi internals,
> 
> As a follow-up on my previous message here
> (https://marc.info/?l=php-internals&m=168912164828942&w=2),
> I would like to start the discussion on the "Support optional suffix
> parameter in tempnam" RFC.
> https://wiki.php.net/rfc/tempnam-suffix-v2

Hello,

The RFC does not explain what benefits brings using tempnam($directory, $prefix, $suffix) over using tempnam($directory, $prefix).$suffix , which you can already use?

Côme

---

## #120898 — tim@bastelstu.be — 2023-08-14T17:21:35+00:00

URL: https://externals.io/message/120893#120898 · In reply to: #120897

Hi


> The RFC does not explain what benefits brings using tempnam($directory, $prefix, $suffix) over using tempnam($directory, $prefix).$suffix , which you can already use?

As per the documentation of tempnam:

> Creates a file with a unique filename, with access permission set to 0600, in the specified directory.

The function does not just generate a filename, but also creates the 
file itself (with strict permissions) to "reserve" it and thus 
*guarantees* that there won't be a conflict. Manually appending a suffix 
does not have that property [1].

Best regards
Tim Düsterhus

[1] Though I'd like to note that using `$prefix . 
bin2hex(random_bytes(16)) . $suffix` as the filename will almost 
certainly [2] give you a conflict-free filename and doesn't have 
OS-specific behavior. That's why I personally don't bother with `tempnam()`.

[2] Collision chance is less than 10^(-7) for 2^52 files.

---

## #120899 — Athos Ribeiro — 2023-08-14T17:49:36+00:00

URL: https://externals.io/message/120893#120899 · In reply to: #120897

>Le mardi 8 août 2023, 21:11:01 CEST Athos Ribeiro a écrit :
> > […]
>
>Hello,
>
>The RFC does not explain what benefits brings using tempnam($directory, $prefix, $suffix) over using tempnam($directory, $prefix).$suffix , which you can already use?
>
>Côme

Hi Côme,

Thanks for the input and for going through the proposal.

As the tempnam short description
(https://www.php.net/manual/en/function.tempnam.php) states

   tempnam — __Create__ file with unique file name

So tempnam

1) creates a file with specific permissions in a given path
($directory); and

2) returns the full path of the created file on success.

For the sole purpose of obtaining a file path (2), I see no reasons for
not using "tempnam($directory, $prefix).$suffix" as you suggested.

However, to create the temporary file (1) allowing appending a suffix
string to the created file, we would need to change tempnam. This is the
purpose of this RFC.

In other words, although the return value of "tempnam($directory,
$prefix).$suffix" and the proposed "tempnam($directory, $prefix,
$suffix)" __may__ be the same, the outcome of running each of those, is
not.

The second paragraph of the RFC introduction says

   "It would be useful to allow users to also specify a suffix when
   __generating__ temporary files. [...]"

Do you have any suggestions on how we could improve the RFC introduction
text to make this clearer to readers of this RFC?

-- 
Athos Ribeiro

---

## #120929 — Athos Ribeiro — 2023-08-23T19:11:31+00:00

URL: https://externals.io/message/120893#120929 · In reply to: #120893

>I would like to start the discussion on the "Support optional suffix
>parameter in tempnam" RFC.
>https://wiki.php.net/rfc/tempnam-suffix-v2

Hi,

As per item 6 at https://wiki.php.net/rfc/howto#how_to_create_an_rfc,
this is a heads up to announce I am moving this to the voting phase
tomorrow.

-- 
Athos Ribeiro
