/* *$path文件夾路徑 *$filename壓縮文件路徑 */ public function zipupload($path,$filename,$f,$f2,$baseinfo){ $path = iconv("UTF-8", "GBK", $path);//加這行中文文件夾也ok了 $this->create_zip($path,$filename); if(!file_exists('./' . $filename . '.zip')){ echo 1;die; } file_put_contents($f,str_replace($this->auth->id,'#userId', file_get_contents($f))); file_put_contents($f,str_replace( $baseinfo['appId'],'#appId',file_get_contents($f))); file_put_contents($f,str_replace($baseinfo['appKey'],'#appKey', file_get_contents($f))); file_put_contents($f2,str_replace( $this->auth->id,'#userId',file_get_contents($f2))); file_put_contents($f2,str_replace($baseinfo['appId'],'#appId', file_get_contents($f2))); file_put_contents($f2,str_replace($baseinfo['appKey'],'#appKey', file_get_contents($f2))); header("Cache-Control: public"); header("Content-Description: File Transfer"); header('Content-disposition: attachment; filename=' . basename($filename . '.zip')); //文件名 header("Content-Type: application/zip"); //zip格式的 header("Content-Transfer-Encoding: binary"); //告訴瀏覽器,這是二進制文件 header('Content-Length: ' . filesize('./' . $filename. '.zip')); //告訴瀏覽器,文件大小 ob_clean(); flush(); @readfile('./' . $filename . '.zip');//下載到本地 @unlink('./' . $filename . '.zip');//刪除服務器上生成的這個壓縮文件 } function create_zip($path,$filename){ $zip = new \ZipArchive(); if($zip->open($filename.'.zip', \ZipArchive::CREATE | \ZipArchive::OVERWRITE)) { $this->addFileToZip($path, $zip);//調用方法,對要打包的根目錄進行操作,並將ZipArchive的對象傳遞給方法 $zip->close(); //關閉處理的zip文件 } } function addFileToZip($path,$zip){ $handler=opendir($path); //打開當前文件夾由$path指定。 while(($filename=readdir($handler))!==false){ if($filename != "." && $filename != ".."){//文件夾文件名字為'.'和‘..’,不要對他們進行操作 if(is_dir($path."/".$filename)){ $this->addFileToZip($path."/".$filename, $zip); }else{ $zip->addFile($path."/".$filename); } } } @closedir($path); } /** * 文件夾文件拷貝 * * @param string $src 來源文件夾 * @param string $dst 目的地文件夾 * @return bool */ function dir_copy($src = '', $dst = '') { if (empty($src) || empty($dst)) { return false; } $dir = opendir($src); $this->dir_mkdir($dst); while (false !== ($file = readdir($dir))) { if (($file != '.') && ($file != '..')) { if (is_dir($src . '/' . $file)) { $this->dir_copy($src . '/' . $file, $dst . '/' . $file); } else { copy($src . '/' . $file, $dst . '/' . $file); } } } closedir($dir); return true; } /** * 創建文件夾 * * @param string $path 文件夾路徑 * @param int $mode 訪問權限 * @param bool $recursive 是否遞歸創建 * @return bool */ function dir_mkdir($path = '', $mode = 0777, $recursive = true) { clearstatcache(); if (!is_dir($path)) { mkdir($path, $mode, $recursive); return chmod($path, $mode); } return true; } //刪除指定文件夾以及文件夾下的所有文件 function deldir($dir) { //先刪除目錄下的文件: $dh=opendir($dir); while ($file=readdir($dh)) { if($file!="." && $file!="..") { $fullpath=$dir."/".$file; if(!is_dir($fullpath)) { unlink($fullpath); } else { deldir($fullpath); } } } closedir($dh); //刪除當前文件夾: if(rmdir($dir)) { return true; } else { return false; } }