如何使用php将文件添加到ziparchive中新创建的文件夹? [英] How to add files to newly created folder in ziparchive using php?
问题描述
如上图所示,我的图像组织在虚拟文件夹中(在 mysql 数据库中,但不是真实文件夹中).我需要使所选文件夹可用于下载为 zip.我能够压缩图像,但我们如何添加子目录以便向其中添加图像(我尝试使用 addEmptyDir()
但无法想出一种向其中添加图像的方法!).如果不创建物理文件夹,这真的可能吗?
请推荐!
感谢@dnagirl 的回答,但我实际上正在寻找一种在子文件夹中添加文件的方法(也许我无法说清楚!)>
发现如果我们给 'path/to/filename
' 而不是 'filename
' ,zipArchive 会自动创建文件夹
这就是我实现它的方式,以防有人需要它!
function zipFilesAndDownload($file_names,$archive_file_name,$img_localpath){$folder_path=WSP_AK_PLUGIN_DIR ."akTempFolder/";$zip = 新的 ZipArchive();if ($zip->open($folder_path.$archive_file_name, ZIPARCHIVE::CREATE)===TRUE) {//$zip->addEmptyDir('myimages');(这就是我想创建的文件夹!)foreach($file_names 作为 $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("内容处理:附件;文件名=$archive_file_name");header('内容类型:应用程序/强制下载');header('Content-Length:' .filesize($folder_path.$archive_file_name));header("Pragma: no-cache");header("过期:0");如果(文件存在($folder_path.$archive_file_name)){ob_clean();读取文件($folder_path.$archive_file_name);}取消链接($folder_path.$archive_file_name);}}
As in the image above ,I have images that are organized in virtual folders(in mysql database but not real folders). I need to make the selected folder available for download as zip .I was able to zip the images but how can we add subdirectory so as to add images to it (i tried using addEmptyDir()
but couldn't figure out a way to add images into it! ).
Is this really possible without creating the physical folder?
Please suggest!
Thanks @dnagirl for your answer, but I was actually looking for a way to add files inside subfolders (maybe I was unable to make myself clear!)
discovered that zipArchive automatically creates folder if we give 'path/to/filename
' instead of just 'filename
'
This is how I achieved it in case anybody may need it!
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();
in above code $files['folder_path']
will give the path to file where it will be created For eg 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);
}
}
这篇关于如何使用php将文件添加到ziparchive中新创建的文件夹?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!