我试图将我的图像保存在一个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);
?>
答案 0 :(得分:0)
简单地改变:
$zip->addFile($file_path.$files,$files);
成:
$zip->addFile($file_path.$files, 'pictures' . DIRECTORY_SEPARATOR . pathinfo($files)['basename']);
addFile
方法的第二个参数是文件在存档中的外观,因此您可以定义新的路径和文件名。
答案 1 :(得分:0)
如果我说对了,你想在zip存档中找到文件夹'images'。
尝试这样的事情:
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中的文件路径