我需要在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时,如果要包含到zip中的文件路径是path / to / file / filename.pdf,我将把filename.pdf包含在path / to / file文件夹中。如何将所有文件放在zip的根目录中?