如上图所示,我有组织在虚拟文件夹中的图像(在 mysql 数据库中,但不是真实文件夹中)。我需要使所选文件夹可供使用下载为 zip 文件.我能够压缩图像,但是我们如何添加子目录以便向其中添加图像(我尝试使用addEmptyDir()
但无法找到向其中添加图像的方法! )。
如果不创建物理文件夹,这真的可能吗?
请建议!
感谢@dnagirl 的回答,但我实际上正在寻找一种在子文件夹中添加文件的方法(也许我无法说清楚!)
发现如果我们给出 ' zipArchive 会自动创建文件夹path/to/filename
' 而不仅仅是 'filename
'
这就是我实现它的方法,以防有人需要它!
function zipFilesAndDownload($file_names,$archive_file_name,$img_localpath)
{
$folder_path=WSP_AK_PLUGIN_DIR ."akTempFolder/";
$zip = new ZipArchive();
if ($zip->open($folder_path.$archive_file_name, ZIPARCHIVE::CREATE )===TRUE) {
//$zip->addEmptyDir('myimages');(this was what i thought would create folder!)
foreach($file_names as $files)
{
//$zip->addFile($img_localpath.'/'.$files, 'myfolder/'.$files);
$zip->addFile($img_localpath.'/'.$files['fileName'], $files['folder_path'].$files['fileName']);
}
$zip->close();
在上面的代码中$files['folder_path']
将给出将在其中创建文件的路径,例如folder1/folder2/image1.jpg
header("Content-type: application/zip");
header("Content-Disposition: attachment; filename=$archive_file_name");
header('Content-Type: application/force-download');
header('Content-Length: ' . filesize($folder_path.$archive_file_name));
header("Pragma: no-cache");
header("Expires: 0");
if( file_exists($folder_path.$archive_file_name)) {
ob_clean();
readfile($folder_path.$archive_file_name);
}
unlink($folder_path.$archive_file_name);
}
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)