duande3134 2016-08-23 08:33
浏览 45

没有路径的Zip内的文件

I create a zip file with php with this code:

<?php
$file1 = '/home/##ACCOUNT##/public_html/##SITE##/images/uploadfiles/backup/file1.csv';
$file2 = '/home/##ACCOUNT##/public_html/##SITE##/images/uploadfiles/backup/file2.csv';

// CREATE THE ZIP
$files = array($file1, $file2);
$zipname = '/home/##ACCOUNT##/public_html/##SITE##/images/uploadfiles/backup/backup.zip';
$zip = new ZipArchive;
$zip->open($zipname, ZipArchive::CREATE);
foreach ($files as $file) {
$zip->addFile($file);
}
$zip->close();
?>

In the created zip i get 2 files as i want it but it also contains the tree/path of those files so opening the zip you have to navigate inside those folder to get to the files is there a away to just have the 2 files in the zip file1.csv & file2.csv without the folders?

I found the anwser i wanted in another post php creating zips without path to files inside the zip

  • 写回答

2条回答 默认 最新

  • dpbf62565 2016-08-23 08:38
    关注

    You can use ZipArchive::renameName method to change name of the file being added or use ZipArchive::addFromString. In the second case you need to remember that you first need to read that file into memory, so it should only be used in consideration with memory available.

    Also you can use second parameter to ZipArchive::addFile as suggested in other answer.

    foreach ($files as $file) {
        $localName = basename($file);
    
        $zip->addFile($file, $localname);
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度