linux安装rar扩展
原创 linux rar 2020-08-19 12:26:26
2 收藏 2 浏览1310 评论0
琴朗
已发33篇
有侵犯版权请及时联系我们,我们将在24小时内删除文章。
投诉举报


扩展地址  http://pecl.php.net/package/rar


安装rar扩展

目前最新版本是2020-12-06号发布的4.2.0,根据最新更新安装即可

wget http://pecl.php.net/get/rar-4.2.0.tgz 
gunzip rar-4.2.0.tgz
tar -xvf rar-4.2.0.tar
cd rar-4.2.0
phpize
./configure && make && make install
# 报错
configure: error: Cannot find php-config. Please use --with-php-config=PATH
# 运行./configure 时指定php-config路径即可
./configure --with-php-config=/www/server/php/70/bin/php-config
make && make install


配置rar扩展

在解压目录里执行phpize主要是为了侦测当前php运行环境,给rar源码生成configure配置的相关文件,以便后期编译需要。

运行configure ,注意后面的--with-php-config参数一定不能缺,这个的作用是关联上当前需要关联的php版本,尤其是在安装多个不同版本php时。

./configure --with-php-config=/www/server/php/72/bin/php-config

编译源码

make&&make install


编译完成在最后会提示生成的rar.so所在的路径。

Buildcomplete.

Don't forget to run 'make test'.

Installing shared extensions: /www/server/php/72/lib/php/extensions/no-debug-non-zts-20170718/

在php7.2的配置文件php.ini最后一行添加

[rar]

extension="/www/server/php/72/lib/php/extensions/no-debug-non-zts-20170718/rar.so"

重启一下相应版本的php7.2 fpm服务,或者干脆重启一下服务器。

检查一下php7.2是否加载rar扩展成功

/www/server/php/72/bin/php-m

看到执行命令后返回的文字里面有rar,成功!


PHP解压rar文件方法

/**
 * 解压rar文件到指定目录
 * $filepath: 文件路径
 * $extractTo: 解压路径
 */
function unrar_file($filepath,$extractTo) {
    $rar_file = rar_open($filepath) or die('无法打开文件');
    $list = rar_list($rar_file) or die('如何获得列表');
    foreach($list as $file) {
        $pattern = '/\".*\"/';
        preg_match($pattern, $file, $matches, PREG_OFFSET_CAPTURE);
        $pathStr=$matches[0][0];
        $pathStr=str_replace("\"",'',$pathStr);
        $entry = rar_entry_get($rar_file, $pathStr) or die('</br>entry not found');
        $entry->extract($extractTo); // extract to the current dir
    }
    rar_close($rar_file);
}


评论(0) 我要评论