8000 feature #33003 [Filesystem] Add $suffix argument to tempnam() (jdufre… · symfony/symfony@7784d9f · GitHub
[go: up one dir, main page]

Skip to content

Commit 7784d9f

Browse files
committed
feature #33003 [Filesystem] Add $suffix argument to tempnam() (jdufresne)
This PR was submitted for the 4.4 branch but it was merged into the 5.1-dev branch instead (closes #33003). Discussion ---------- [Filesystem] Add $suffix argument to tempnam() | Q | A | ------------- | --- | Branch? | 4.4 | Bug fix? | no | New feature? | yes | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | #33002 | License | MIT | Doc PR | symfony/symfony-docs#12108 **Description** The `tempnam()` interface was previously: ```php tempnam($dir, $prefix) ``` This adds a third argument, `$suffix`, that is appended to the filename after the random component. This defaults to `''` for backwards compatibility. This is quite useful when the temporary file is consumed for a specific purpose that expects a suffix. **Example** ```php $filesystem->tempnam('/tmp', 'prefix_', '.png'); ``` Would create a file like: `/tmp/prefix_abcd1234.png`. Commits ------- ef12069 [Filesystem] Add $suffix argument to tempnam()
2 parents 8e22719 + ef12069 commit 7784d9f

File tree

3 files changed

+22
-3
lines changed

3 files changed

+22
-3
lines changed

src/Symfony/Component/Filesystem/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ CHANGELOG
1010
-----
1111

1212
* support for passing a `null` value to `Filesystem::isAbsolutePath()` is deprecated and will be removed in 5.0
13+
* `tempnam()` now accepts a third argument `$suffix`.
1314

1415
4.3.0
1516
-----

src/Symfony/Component/Filesystem/Filesystem.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -584,15 +584,17 @@ public function isAbsolutePath(string $file)
584584
*
585585
* @param string $prefix The prefix of the generated temporary filename
586586
* Note: Windows uses only the first three characters of prefix
587+
* @param string $suffix The suffix of the generated temporary filename
587588
*
588589
* @return string The new temporary filename (with path), or throw an exception on failure
589590
*/
590-
public function tempnam(string $dir, string $prefix)
591+
public function tempnam(string $dir, string $prefix/*, string $suffix = ''*/)
591592
{
593+
$suffix = \func_num_args() > 2 ? func_get_arg(2) : '';
592594
list($scheme, $hierarchy) = $this->getSchemeAndHierarchy($dir);
593595

594596
// If no scheme or scheme is "file" or "gs" (Google Cloud) create temp file in local filesystem
595-
if (null === $scheme || 'file' === $scheme || 'gs' === $scheme) {
597+
if ((null === $scheme || 'file' === $scheme || 'gs' === $scheme) && '' === $suffix) {
596598
$tmpFile = @tempnam($hierarchy, $prefix);
597599

598600
// If tempnam failed or no scheme return the filename otherwise prepend the scheme
@@ -610,7 +612,7 @@ public function tempnam(string $dir, string $prefix)
610612
// Loop until we create a valid temp file or have reached 10 attempts
611613
for ($i = 0; $i < 10; ++$i) {
612614
// Create a unique filename
613-
$tmpFile = $dir.'/'.$prefix.uniqid(mt_rand(), true);
615+
$tmpFile = $dir.'/'.$prefix.uniqid(mt_rand(), true).$suffix;
614616

615617
// Use fopen instead of file_exists as some streams do not support stat
616618
// Use mode 'x+' to atomically check existence and create to avoid a TOCTOU vulnerability

src/Symfony/Component/Filesystem/Tests/FilesystemTest.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1509,6 +1509,22 @@ public function testTempnamOnUnwritableFallsBackToSysTmp()
15091509
@unlink($filename);
15101510
}
15111511

1512+
public function testTempnamWithSuffix()
1513+
{
1514+
$dirname = $this->workspace;
1515+
$filename = $this->filesystem->tempnam($dirname, 'foo', '.bar');
1516+
$this->assertStringEndsWith('.bar', $filename);
1517+
$this->assertFileExists($filename);
1518+
}
1519+
1520+
public function testTempnamWithSuffix0()
1521+
{
1522+
$dirname = $this->workspace;
1523+
$filename = $this->filesystem->tempnam($dirname, 'foo', '0');
1524+
$this->assertStringEndsWith('0', $filename);
1525+
$this->assertFileExists($filename);
1526+
}
1527+
15121528
public function testDumpFile()
15131529
{
15141530
$filename = $this->workspace.\DIRECTORY_SEPARATOR.'foo'.\DIRECTORY_SEPARATOR.'baz.txt';

0 commit comments

Comments
 (0)
0