2017-07-05 22 views
2

イメージを持つzipを作成するためのスクリプトがあります。それは本当にうまくいく!PHPで作成したzipファイルの名前を変更します

create_zip($files_to_zip, 'zip/download-'.$id_account.'-'.$time.'.zip'); 

function create_zip($files = array(),$destination = '',$overwrite = true) { 

     if(file_exists($destination) && !$overwrite) { return false; } 
     $valid_files = array(); 
     if(is_array($files)) { 
      foreach($files as $file) { 
       if(file_exists($file)) { 
        $valid_files[] = $file; 
       } 
      } 
     } 
     if(count($valid_files)) { 
      $zip = new ZipArchive(); 
      if($zip->open($destination,$overwrite ? ZIPARCHIVE::OVERWRITE : ZIPARCHIVE::CREATE) !== true) { 
       return false; 
      } 
      foreach($valid_files as $file) { 
       $zip->addFile($file,$file); 
      } 
      $zip->close(); 

      return file_exists($destination); 
     } 
     else { return false; } 
    } 

私は、ファイルをダウンロードすると、それがよくzip/download-'.$id_account.'-'.$time.'.zip という名前だが、私はこれを抽出する際に、抽出フォルダは常にfilesという名前です。 result

解凍したファイルの名前を別の名前に変更してください。

ありがとうございます!

答えて

0

このコードを使用すると、zipファイルを作成できます。

はあなただけヘッダ部分を逃し、あなたの場合には、この

$File = 'path' . time() . '.zip'; 
$zip = new \ZipArchive(); 
$res = $zip->open($File, \ZipArchive::CREATE); 

if ($res === TRUE) { 
    $zip->addFile($filename); 
    $zip->close(); 
} 
header("Content-Type: application/zip"); 
header("Content-Disposition: attachment; filename=\"" . basename($File) . "\""); 
header("Content-Length: " . filesize($File)); 
header("Pragma: no-cache"); 
header("Expires: 0"); 
readfile($File); 
header("Connection: close"); 

好きです。
コードにヘッダー部分を追加するだけで、うまくいくでしょう。

関連する問題