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.