php压缩同一目录中的所有文件和文件夹
如何使用php zip压缩同一目录中的所有文件和文件夹php压缩同一目录中的所有文件和文件夹,php,zip,Php,Zip,如何使用php zip压缩同一目录中的所有文件和文件夹 所以基本上我有一个文件和文件夹的列表,我想用php压缩所有东西。抱歉,我不熟悉php zip,php net上的文档帮助不大您可以使用此功能创建文件的zip文件夹: public function create_zip($files = array(),$destination = '',$overwrite = false) { //if the zip file already exists and overwrite is f
所以基本上我有一个文件和文件夹的列表,我想用php压缩所有东西。抱歉,我不熟悉php zip,php net上的文档帮助不大您可以使用此功能创建文件的zip文件夹:
public function create_zip($files = array(),$destination = '',$overwrite = false) {
//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)) {
//create the archive
$zip = new ZipArchive();
if($zip->open($destination,$overwrite ? ZIPARCHIVE::OVERWRITE : ZIPARCHIVE::CREATE) !== true) {
return false;
}
//add the files
foreach($valid_files as $file) {
$zip->addFile($file,$file);
}
//debug
//echo 'The zip archive contains ',$zip->numFiles,' files with a status of ',$zip->status;
//close the zip -- done!
$zip->close();
//check to make sure the file exists
return file_exists($destination);
}
else
{
return false;
}
}
此处,$files将包含数组中的所有文件。$destination将是您在服务器上压缩文件的位置。Ronak我知道如何压缩特定的文件,我想压缩所有文件和文件夹,使其成为主文件夹的备份