function Filesystem::tempnam
Same name in this branch
- 11.1.x core/lib/Drupal/Core/File/FileSystem.php \Drupal\Core\File\FileSystem::tempnam()
Creates a temporary file with support for custom stream wrappers.
Parameters
string $prefix The prefix of the generated temporary filename: Note: Windows uses only the first three characters of prefix
string $suffix The suffix of the generated temporary filename:
Return value
string The new temporary filename (with path), or throw an exception on failure
1 call to Filesystem::tempnam()
- Filesystem::dumpFile in vendor/
symfony/ filesystem/ Filesystem.php - Atomically dumps content into a file.
File
-
vendor/
symfony/ filesystem/ Filesystem.php, line 601
Class
- Filesystem
- Provides basic utility to manipulate the file system.
Namespace
Symfony\Component\FilesystemCode
public function tempnam(string $dir, string $prefix, string $suffix = '') : string {
[
$scheme,
$hierarchy,
] = $this->getSchemeAndHierarchy($dir);
// If no scheme or scheme is "file" or "gs" (Google Cloud) create temp file in local filesystem
if ((null === $scheme || 'file' === $scheme || 'gs' === $scheme) && '' === $suffix) {
// If tempnam failed or no scheme return the filename otherwise prepend the scheme
if ($tmpFile = self::box('tempnam', $hierarchy, $prefix)) {
if (null !== $scheme && 'gs' !== $scheme) {
return $scheme . '://' . $tmpFile;
}
return $tmpFile;
}
throw new IOException('A temporary file could not be created: ' . self::$lastError);
}
// Loop until we create a valid temp file or have reached 10 attempts
for ($i = 0; $i < 10; ++$i) {
// Create a unique filename
$tmpFile = $dir . '/' . $prefix . bin2hex(random_bytes(4)) . $suffix;
// Use fopen instead of file_exists as some streams do not support stat
// Use mode 'x+' to atomically check existence and create to avoid a TOCTOU vulnerability
if (!($handle = self::box('fopen', $tmpFile, 'x+'))) {
continue;
}
// Close the file if it was successfully opened
self::box('fclose', $handle);
return $tmpFile;
}
throw new IOException('A temporary file could not be created: ' . self::$lastError);
}