FileSystem::tempnam() doesn't respect subdirectories for stream wrappers

Created on 30 November 2010, almost 14 years ago
Updated 30 January 2023, almost 2 years ago

Problem/Motivation

When a stream wrapper path (e.g. private://some-path, temporary://some-path, etc) is passed as the $directory parameter to
FileSystem::tempnam() (or drupal_tempnam() in D7), the unique filename returned is always in the root path of the file system represented by the stream wrapper without any of the subdirectory paths included in the $directory parameter.

Steps to reproduce

Create a filename for stream wrapper path using FileSystem::tempnam():

// Create a filename in '/tmp/some-path'.
$filename = \Drupal::service('file_system')->tempnam('temporary://some-path', 'prefix_');

Expected $filename value: /tmp/some-path/prefix_random-filename
Actual $filename value: /tmp/prefix_random-filename

Proposed resolution

Modify FileSystem::tempnam() to return a filename that includes any subdirectories passed as part of $directory parameter containing stream wrapper paths.

Remaining tasks

Review submited patches.

User interface changes

None.

API changes

Currently if stream wrapper path including a non-existant / invalid subdirectory (e.g. private://non-existant-path) is passed as the $directory parameter to FileSystem::tempnam() a valid filename in the root path for the stream wrapper will be returned. After the proposed changes, FileSystem::tempname() will return FALSE when a stream wrapper path containing invalid subdirectories is used.

Data model changes

None.

Original report by Steven Jones

I was trying to do this:

// Create a file in '/tmp/some-path'.
$file = drupal_tempnam('temporary://some-path', 'prefix');

But the returned filename will always reside in the base directory of the 'temporary' wrapper, because the implementation of drupal_tempnam uses the wrappers getDirectoryPath method, which basically returns the root of the filesystem represented by the wrapper.

Is this on purpose? Or a bug that needs fixing?
If the former, the docs need changing.

🐛 Bug report
Status

Needs work

Version

10.1

Component
File system 

Last updated about 3 hours ago

Created by

🇬🇧United Kingdom steven jones

Live updates comments and jobs are added and updated live.
  • Needs backport to D7

    After being applied to the 8.x branch, it should be considered for backport to the 7.x branch. Note: This tag should generally remain even after the backport has been written, approved, and committed.

Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • The Needs Review Queue Bot tested this issue. It either no longer applies to Drupal core, or fails the Drupal core commit checks. Therefore, this issue status is now "Needs work".

    Apart from a re-roll or rebase, this issue may need more work to address feedback in the issue or MR comments. To progress an issue, incorporate this feedback as part of the process of updating the issue. This helps other contributors to know what is outstanding.

    Consult the Drupal Contributor Guide to find step-by-step guides for working with issues.

Production build 0.71.5 2024