【问题标题】:zip with php in root directory在根目录中使用 php 压缩
【发布时间】:2013-07-09 18:36:31
【问题描述】:

我需要在 php 中创建一个 zip 文件,其中包括一些 doc 和 pdf 文件。我使用了一个在网上找到的函数:

function create_zip($files = array(),$destination = '',$overwrite = true) {
    //if the zip file already exists and overwrite is false, return false
    if(file_exists($destination) && !$overwrite) { return false; }
    //vars
    $valid_files = array();
    //if files were passed in...
    if(is_array($files)) {
        //cycle through each file
        foreach($files as $file) {
            //make sure the file exists
            if(file_exists($file)) {
                $valid_files[] = $file;
            }
        }
    }
    //if we have good files...
    if(count($valid_files)) {
        $zip = new ZipArchive();
        if($zip->open($destination,$overwrite ? ZIPARCHIVE::OVERWRITE : ZIPARCHIVE::CREATE) !== true) {
            return false;
        }

        foreach($valid_files as $file)
            $zip->addFile($file,$file);

        $zip->close();

        return file_exists($destination);
    }
    else
        return false;
}

问题是,如果要包含在 zip 中的文件路径是 path/to/file/filename.pdf,当我下载并打开 zip 时,我会将 filename.pdf 包含在 path/to/file 文件夹中。如何将所有文件放在 zip 的根目录中?

【问题讨论】:

    标签: php zip directory


    【解决方案1】:

    使用ZipArchive::addFile() 的第二个参数指定文件在存档中出现的文件名。 basename($file) 是一个很好的候选人。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-11-13
      • 2011-01-29
      • 2020-10-30
      • 2012-12-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-05-06
      相关资源
      最近更新 更多