使用php创建具有特定扩展名的临时文件

时间:2020-12-17 23:55:35

How do I create a temporary file with a specified extension in php. I came across tempnam() but using it the extension can't be specified.

如何在php中创建具有指定扩展名的临时文件。我遇到了tempnam()但是使用它时无法指定扩展名。

5 个解决方案

#1


6  

This might simulate mkstemp() (see http://linux.die.net/man/3/mkstemp) a bit, achieving what you want to do:

这可能会模拟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;
}

So you could do:

所以你可以这样做:

if( ($f = mkstemp("test-XXXXXX.txt")) ) {
  fwrite($f, "test\n");
  fclose($f);
}

#2


10  

my way is using tempnam

我的方式是使用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

#3


7  

Easiest way i have found is to create tempfile and then just rename it. For example:

我找到的最简单的方法是创建临时文件,然后重命名它。例如:

 $tmpfname = tempnam(sys_get_temp_dir(), "Pre_");
 rename($tmpfname, $tmpfname .= '.pdf');

#4


0  

Maybe using

move_uploaded_file($tmp_name, "$uploads_dir/$name.myextension");

See http://php.net/manual/en/function.move-uploaded-file.php#example-2209

#5


0  

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;
}

#1


6  

This might simulate mkstemp() (see http://linux.die.net/man/3/mkstemp) a bit, achieving what you want to do:

这可能会模拟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;
}

So you could do:

所以你可以这样做:

if( ($f = mkstemp("test-XXXXXX.txt")) ) {
  fwrite($f, "test\n");
  fclose($f);
}

#2


10  

my way is using tempnam

我的方式是使用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

#3


7  

Easiest way i have found is to create tempfile and then just rename it. For example:

我找到的最简单的方法是创建临时文件,然后重命名它。例如:

 $tmpfname = tempnam(sys_get_temp_dir(), "Pre_");
 rename($tmpfname, $tmpfname .= '.pdf');

#4


0  

Maybe using

move_uploaded_file($tmp_name, "$uploads_dir/$name.myextension");

See http://php.net/manual/en/function.move-uploaded-file.php#example-2209

#5


0  

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;
}