dsztc99732 2016-03-17 13:30
浏览 34
已采纳

在php中的自定义文件夹上将图像保存为zip

im trying to save my images on a zip file, my code works fine but saves all images inside the folder /wp-content/uploads/2016/03/... and i want to have my own "images" folder on the zip with all the content on it... i tried the other solutions posted on stackoverflow but i cant get it to work

my code :

<?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>
");
    }
    //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);
?>
  • 写回答

2条回答 默认 最新

  • douwei7203 2016-03-17 13:58
    关注

    Simply change:

    $zip->addFile($file_path.$files,$files);
    

    into:

    $zip->addFile($file_path.$files, 'pictures' . DIRECTORY_SEPARATOR . pathinfo($files)['basename']);
    

    Second parameter for addFile method is how file will look like inside archive so you can define new path and filename.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 关于#java#的问题:找一份能快速看完mooc视频的代码
  • ¥15 这种微信登录授权 谁可以做啊
  • ¥15 请问我该如何添加自己的数据去运行蚁群算法代码
  • ¥20 用HslCommunication 连接欧姆龙 plc有时会连接失败。报异常为“未知错误”
  • ¥15 网络设备配置与管理这个该怎么弄
  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!