在 php 中的自定义文件夹中将图像另存为 zip
save images as zip on a custom folder in php
我试图将我的图像保存在一个 zip 文件中,我的代码工作正常,但将所有图像保存在文件夹 /wp-content/uploads/2016/03/... 中,我想拥有自己的 "images" 文件夹包含所有内容的 zip...我尝试了 Whosebug 上发布的其他解决方案,但我无法让它工作
我的代码:
<?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
方法的第二个参数是文件在存档中的样子,因此您可以定义新的路径和文件名。
如果我没记错的话,你需要文件夹 'images' inside zip archive。
尝试这样的事情:
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 中的文件路径
我试图将我的图像保存在一个 zip 文件中,我的代码工作正常,但将所有图像保存在文件夹 /wp-content/uploads/2016/03/... 中,我想拥有自己的 "images" 文件夹包含所有内容的 zip...我尝试了 Whosebug 上发布的其他解决方案,但我无法让它工作
我的代码:
<?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
方法的第二个参数是文件在存档中的样子,因此您可以定义新的路径和文件名。
如果我没记错的话,你需要文件夹 'images' inside zip archive。
尝试这样的事情:
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 中的文件路径