![]() |
流觞的嗜好 | 2017-06-07 14:29:48 |
如何使用PHP解压RAR文件呢?
首先要添加PHP的rar扩展,下载地址:http://pecl.php.net/package/rar
然后在PHP.ini文件中添加一条语句:extension=php_rar.dll
可以使用一下代码:
//解压RAR压缩文件到文件夹方法 function unrartofile($rarfile,$filepath=null){ set_time_limit(0);//设置执行时间为无限期 $a = date("Ymd"). "/" . date("His") . "/"; $filepath = "./upload/show/" . $a; if(!file_exists($filepath)){ mkdir($filepath,0777,true); } $rar_file = rar_open($rarfile) or die('could not open rar'); $list = rar_list($rar_file) or die('could not get list'); // print_r($list); foreach($list as $file) { $pattern = '/\".*\"/'; preg_match($pattern, $file, $matches, PREG_OFFSET_CAPTURE); $pathStr=$matches[0][0]; $pathStr=str_replace("\"",'',$pathStr); // print_r($pathStr); $entry = rar_entry_get($rar_file, $pathStr) or die('</br>entry not found'); $entry->extract($filepath); // extract to the current dir } rar_close($rar_file); }