1、下载对应php版本https://pecl.php.net/package/rar/4.2.0/windows
2.$filepath上传的压缩包路径,$extractTo解压缩路径
public function unrar($filepath,$extractTo){
$rar_file = rar_open($filepath) or die("Failed to open Rar archive");
$entries = rar_list($rar_file);
foreach ($entries as $entry) {
$entry->extract($extractTo); /*/dir/extract/to/换成其他路径即可*/
}
rar_close($rar_file);
}
3.解压缩前可能需要将原文件删除,
//$dir文件路径
public function deldir($dir) {
//先删除目录下的文件:
$dh=opendir($dir);
while ($file=readdir($dh)) {
if($file!="." && $file!="..") {
$fullpath=$dir."/".$file;
if(!is_dir($fullpath)) {
unlink($fullpath);
} else {
$this->deldir($fullpath);
}
}
}
closedir($dh);
//删除当前文件夹:
if(@rmdir($dir)) {
return true;
} else {
return false;
}
}