普通网友 2013-07-11 13:07
浏览 83
已采纳

在根目录下用php压缩

I need to create a zip file in php, including in it some doc and pdf files. I use a function found on the web:

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;
}

The problem is that if the file path to include into the zip is path/to/file/filename.pdf when I download and open the zip i get filename.pdf contained into path/to/file folders. How can I do to put all the files in the root of the zip?

  • 写回答

1条回答 默认 最新

  • doushi5752 2013-07-11 13:11
    关注

    Use the second parameter of ZipArchive::addFile() to specify under which file name the file appears in the archive. basename($file) is a good candidate for that.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!