如何在php中创建具有指定扩展名的临时文件。我遇到了tempnam()
,但使用它时无法指定扩展名。
我找到的最简单的方法是创建临时文件,然后重命名它。例如:
$tmpfname = tempnam(sys_get_temp_dir(), "Pre_");
rename($tmpfname, $tmpfname .= '.pdf');
我的方法是使用tempnam
$file = tempnam(sys_get_temp_dir(), 'prefix');
file_put_contents($file.'.extension', $data);
{
//use your file
}
unlink($file);//to delete an empty file that tempnam creates
unlink($file.'.extension');//to delete your file
这可能会模拟mkstemp()
(请参阅http://linux.die.net/man/3/mkstemp)一点,实现你想做的事情:
function mkstemp( $template ) {
$attempts = 238328; // 62 x 62 x 62
$letters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
$length = strlen($letters) - 1;
if( mb_strlen($template) < 6 || !strstr($template, 'XXXXXX') )
return FALSE;
for( $count = 0; $count < $attempts; ++$count) {
$random = "";
for($p = 0; $p < 6; $p++) {
$random .= $letters[mt_rand(0, $length)];
}
$randomFile = str_replace("XXXXXX", $random, $template);
if( !($fd = @fopen($randomFile, "x+")) )
continue;
return $fd;
}
return FALSE;
}
所以你可以做:
if( ($f = mkstemp("test-XXXXXX.txt")) ) {
fwrite($f, "test'n");
fclose($f);
}
假设tempnam()为您提供一个文件"文件名";。你把它移到";filename.ext";。在任何时候,tempnam()都可以给你一个"文件名";再一次如果您检查";filename.ext";,拒绝tempnam()提供的文件名,然后再次调用它,您仍然可能在一个步骤和另一个步骤之间创建一个同名文件。(这在tempnam()文档页面上的用户评论中进行了讨论:https://www.php.net/manual/en/function.tempnam.php.)
然而,如果你只保留由tempnam()创建的文件(在删除"filename.ext"之前不删除"filename"),并使用该文件名+扩展名,那么tempnam)就不可能再次使用该文件名称(据我所见)。是的,有";文件名";以及";filename.ext";对于每个文件。另一方面,它解决了问题。
public static function makeTempFileInFolder($prefix, $suffix, $folder="")
{
if (strlen($folder)==0) $folder = sys_get_temp_dir();
do {
$file = $folder."/".$prefix.rand(1,99999).time().$suffix;
} while (file_exists($file));
return $file;
}
我更喜欢这个解决方案:
$uid = uniqid('', true);
$path = sys_get_temp_dir() . "some_prefix_$uid.myextension";
注意:我不把前缀放在uniqid
中,因为IMHO,它不是它的职责
与tempnam()
相同,除了附加参数:
function tempnamp($dir, $prefix, $postfix) {
$maxAttempts = 1000;
// Trim trailing slashes from $dir.
$dir = rtrim($dir, DIRECTORY_SEPARATOR);
// If we don't have permission to create a directory, fail, otherwise we will
// be stuck in an endless loop.
if (!is_dir($dir) || !is_writable($dir)) return false;
// Make sure characters in prefix and postfix are safe.
if (strpbrk($prefix, '''/:*?"<>|') !== false) return false;
if (strpbrk($postfix, '''/:*?"<>|') !== false) return false;
// Attempt to create a random file until it works.
$attempts = 0;
do
{
$path = $dir.DIRECTORY_SEPARATOR.$prefix.mt_rand(100000, mt_getrandmax()).$postfix;
$fp = @fopen($path, 'x+');
} while (!$fp && $attempts++ < $maxAttempts);
if ($fp) fclose($fp);
return $path;
}
名称末尾的"p"代表"postfix"。
也许使用
move_uploaded_file($tmp_name, "$uploads_dir/$name.myextension");
请参阅http://php.net/manual/en/function.move-uploaded-file.php#example-2209
Rename可以做到这一点,用pathinfo找到扩展名,然后用您想要的扩展名替换。
$tmpfname = tempnam(sys_get_temp_dir(), 'FOO');
$newname = str_replace(pathinfo($tmpfname, PATHINFO_EXTENSION),'pdf',$tmpfname);
rename($tmpfname, $newname);
//do what you want with $newname
unlink($newname);