dongqing6661 2014-02-07 08:56
浏览 74
已采纳

php ZipArchive - 仅添加不是父文件夹的文件

I am creating a zip archive in php and want to add some pdf files available in my pdfs_lib folder.I have successfully created the archive but there is a problem that inside the created zip archive, files get added into pdfs_lib folder, which is not what i want.

I want to add pdf files present in pdfs_lib folder directly into the archive instead of adding those files inside pdfs_lib folder in my created zip archive.

What i have tried is:

    // Create Zip File
    $zip = new ZipArchive;
    $res = $zip->open("pdfs_lib/my_zip_archive.zip", ZipArchive::CREATE);
    if ($res === TRUE)
    {
        // Add pdf file to zip archive
        $zip->addFile("pdfs_lib/pdf_file_1.pdf");
        $zip->addFile("pdfs_lib/pdf_file_2.pdf");

        $zip->close();
        echo 'ok';
    } else {
        echo 'failed';
    }

This code creates the zip archive successfully but whwen i unzip the archive it creates a pdfs_lib folder and unzip all files inside this newly created folder.

Any suggestion on how to create a zip archive without add pdf_lib folder in it.

  • 写回答

2条回答 默认 最新

  • douxi9245 2014-02-07 09:03
    关注

    Change the addFile calls to:

    $zip->addFile("pdfs_lib/pdf_file_1.pdf", "pdf_file_1.pdf");

    The second parameter is the 'localname', the name used in the zipfile itself. If you leave out the folderpath there, it will be added to the root in the zipfile.

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

报告相同问题?

悬赏问题

  • ¥30 YOLO检测微调结果p为1
  • ¥20 求快手直播间榜单匿名采集ID用户名简单能学会的
  • ¥15 DS18B20内部ADC模数转换器
  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题