PHP:下载2创建的文本文件作为Zip


PHP: Downloading 2 Created Text Files As Zip

我试图创建2个文本文件,应该只放在一个可下载的zip文件。

使用两次
fopen("php://output","w")

header() things

可以工作并帮助避免在服务器上创建文件,但我最终将两个字符串混合到一个可下载的文本文件中。

下面是在本地目录中创建2个文件的本地版本:

$dirname = "C:/texts";
if (!is_dir($dirname)) {
    mkdir($dirname);
}
$myfile = fopen("C:/texts/one".$dateInputted.".txt", "w") or die("Unable to open file!");
fwrite($myfile, $one);
fclose($myfile);
$myfile = fopen("C:/texts/two".$dateInputted.".txt", "w") or die("Unable to open file!");
fwrite($myfile, $two);
fclose($myfile);

使用PHP的ZipArchive插件

$zip = new ZipArchive();
$zip->open('path/to/zipfile.zip', ZipArchive::CREATE);
$zip->addFile('some-file.pdf', 'subdir/filename.pdf');
$zip->addFile('another-file.xlxs', 'filename.xlxs');
$zip->close();

你可以在这里找到更多的帮助:

http://akrabat.com/php/creating-a-zip-file-with-phps-ziparchive/

http://php.net/manual/en/zip.examples.php