2017-11-24 12 views
-3

ABC.zipにABCフォルダをしようとしています。 これを変換する方法は?フォルダをZIPファイルに変換する方法

+7

可能な重複[PHPを使用してフォルダ全体を圧縮する方法](https://stackoverflow.com/questions/4914750/how-to-zip -a-whole-folder-using-php)、または[this](https://stackoverflow.com/questions/17708562/zip-all-files-in-directory)などのGoogle上に存在するその他の質問 - と - ダウンロード - ジップ生成)。 – hnefatl

答えて

0

あなたはzipファイルを作成するためにPHP ZIPARCHIVEを使用することができます

$zip = new ZipArchive(); 
$filename = "path/to/save/file/file_name.zip"; 
if ($zip->open($filename, ZipArchive::CREATE) !== TRUE) { 
    exit("cannot open <$filename>\n"); 
} 
$dir = "path/to/folder/for/zip"; 
createZip($zip, $dir); 
$zip->close(); 

function createZip($zip, $dir) { 
if (is_dir($dir)) { 
    if ($dh = opendir($dir)) { 
     while (($file = readdir($dh)) !== false) { 
      // If file 
      if (is_file($dir . $file)) { 
       if ($file != '' && $file != '.' && $file != '..') { 
        $zip->addFile($dir . $file); 
       } else { 
        echo "File is blank"; 
       } 
      } else { 
       // If directory 
       if (is_dir($dir . $file)) { 
        if ($file != '' && $file != '.' && $file != '..') { 
         // Add empty directory 
         $zip->addEmptyDir($dir . $file); 
         $folder = $dir . $file . '/'; 
         // Read data of the folder 
         createZip($zip, $folder); 
        } 
       } else { 
        echo "Invalid dir check"; 
       } 
      } 
     } 
     closedir($dh); 
    } else { 
     echo "Fail to open dir"; 
    } 
} else { 
    echo "Invalid Dir"; 
} 
} 
関連する問題