2

次のようなHTMLコードがあります。

<body>Some text</body>

そして変数$contents

初めてphpでzipを使用する場合、いくつか質問があります。

どうすればよいですか:

  1. 名前の付いたフォルダを作成し、HTMLその中に配置します$contentsftpで実際に作成するのではなく、変数内に配置するだけです)

  2. を作成し、index.htmlその中にHTMLあるフォルダ内に配置します$contents

    したがって、$contentsbeforezipには次のものが含まれている必要があります。

     /HTML/index.html (with <body>Some text</body> code inside)
    
  3. $contents変数内のすべてのコンテンツを含むzipアーカイブを作成します。

4

2 に答える 2

1

私があなたを正しく理解しているなら:

$contents = '/tmp/HTML';
// Make the directory
mkdir($contents);
// Write the html
file_put_contents("$contents/index.html", $html);
// Zip it up
$return_value = -1;
$output = array();
exec("zip -r contents.zip $contents 2>&1", $output, $return_value);
if ($return_value === 0){
    // No errors
    // You now have contents.zip to play with
} else {
   echo "Errors!";
   print_r($output);
}

私はライブラリを使用してzipするのではなく、コマンドラインだけを使用していますが、必要に応じてライブラリを使用することもできます(ただし、zip正しく実行されるかどうかを確認しています)。


本当にすべてをメモリ内で実行したい場合は、次のように実行できます。

$zip = new ZipArchive;
if ($zip->open('contents.zip') === TRUE) {
    $zip->addFromString('contents/index.html', $html);
    $zip->close();
    echo 'ok';
} else {
    echo 'failed';
}

http://www.php.net/manual/en/ziparchive.addfromstring.php

于 2012-07-08T10:06:26.863 に答える
0

ZipArchiveクラスの使用をお勧めします。だからあなたはこのようなものを持つことができます

$html = '<body>some HTML</body>';
$contents = new ZipArchive();
if($contents->open('html.zip', ZipArchive::CREATE)){
    $contents->addEmptyDir('HTML');
    $contents->addFromString('index.html', $html);
    $contents->close()
}
于 2012-07-08T10:31:10.317 に答える