im 尝试将我的图像保存在 zip 文件中,我的代码工作正常,但将所有图像保存在文件夹/wp-content/uploads/2016/03/...我想在 zip 上拥有自己的"图像"文件夹,其中包含所有内容...... 我尝试了在StackOverflow上发布的其他解决方案,但我无法让它工作
我的代码 :
<?php
//This script is developed by www.webinfopedia.com
//For more examples in php visit www.webinfopedia.com
function zipFilesAndDownload($file_names,$archive_file_name,$file_path)
{
$zip = new ZipArchive();
//create the file and throw the error if unsuccessful
if ($zip->open($archive_file_name, ZIPARCHIVE::CREATE )!==TRUE) {
exit("cannot open <$archive_file_name>'n");
}
//add each files of $file_name array to archive
foreach($file_names as $files)
{
$zip->addFile($file_path.$files,$files);
//echo $file_path.$files,$files."<br />";
}
$zip->close();
//then send the headers to foce download the zip file
header("Content-type: application/zip");
header("Content-Disposition: attachment; filename=$archive_file_name");
header("Pragma: no-cache");
header("Expires: 0");
readfile("$archive_file_name");
exit;
}
//------------------------------------------------------------------------------------------------------
//If you are passing the file names to thae array directly use the following method
$file_names = array('../wp-content/uploads/2016/03/IMG_8121-900x1200.jpg','../wp-content/uploads/2016/03/IMG_8124-900x1200.jpg','../wp-content/uploads/2016/03/IMG_8106-900x1200.jpg');
//------------------------------------------------------------------------------------------------------
//------------------------------------------------------------------------------------------------------
//Archive name
$archive_file_name=$name.'DEMOphpCreateZipTodownloadMultipleFiles.zip';
//Download Files path
$file_path=$_SERVER['DOCUMENT_ROOT'].'/images/';
//cal the function
zipFilesAndDownload($file_names,$archive_file_name,$file_path);
?>
只需更改:
$zip->addFile($file_path.$files,$files);
到:
$zip->addFile($file_path.$files, 'pictures' . DIRECTORY_SEPARATOR . pathinfo($files)['basename']);
addFile
方法的第二个参数是文件在存档中的外观,以便您可以定义新的路径和文件名。
如果我
做对了,你想要在zip存档中文件夹"图像"。
尝试这样的事情:
public function zipFilesAndDownload($file_names,$archive_file_name,$file_path)
{
$zip = new 'ZipArchive();
//create the file and throw the error if unsuccessful
if ($zip->open($archive_file_name, ZipArchive::CREATE )!==TRUE) {
exit("cannot open <$archive_file_name>'n");
}
$zip->addEmptyDir("images");
//add each files of $file_name array to archive
foreach($file_names as $files)
{
$zip->addFile($file_path.$files, "images".DIRECTORY_SEPARATOR.$files);
}
$zip->close();
...
}
它将空文件夹广告到您的 zip 中,然后在添加 foreach 循环时指定 zip 中的文件路径