【问题标题】:Creating a zip file in a specific path without any subfolders在没有任何子文件夹的特定路径中创建 zip 文件
【发布时间】:2017-03-27 19:46:57
【问题描述】:

我需要在特定的文件路径中创建一个 Zip 文件,其中没有创建任何子文件夹, 这是我的代码:

$files = array('article/includes/pdfFiles/file1.txt','article/includes/pdfFiles/file2.txt');
$zipname = 'article/includes/pdfFiles/file.zip';
$zip = new ZipArchive;
$zip->open($zipname, ZipArchive::CREATE);
foreach ($files as $file) {
    $zip->addFile($file);
}
$zip->close();

我需要在此文件夹的路径“assets/uploads/pdfFiles”中创建 file.zip,它已创建,但问题出在这些文件夹退出的 zip 文件中:assets/uploads/pdfFiles
如何在没有这些文件夹的情况下创建 zip 文件?

【问题讨论】:

标签: php zip


【解决方案1】:

您上面的代码不起作用,因为您没有显示 $fileNames 的内容,但我假设它类似于:

array('assets/uploads/pdfFiles/file1.txt','assets/uploads/pdfFiles/file2.txt',...)

当您添加到 zip 时,请执行以下操作:

foreach ($fileNames as $fullpath) {
    // pull just the file name from the path
    $filename = substr($fullpath, strrpos($fullpath, '/')+1);
    $zip->addFile($fullpath,$filename);
}

【讨论】:

    猜你喜欢
    • 2011-04-28
    • 2013-07-10
    • 1970-01-01
    • 2021-07-06
    • 2023-02-15
    • 2017-09-19
    • 1970-01-01
    • 2020-05-14
    • 2019-05-17
    相关资源
    最近更新 更多