SoFunction
Updated on 2025-03-10

How to implement rar file reading and decompression in php_rar extension

This article describes the method of installing php_rar extension to implement rar file reading and decompression. Share it for your reference, as follows:

The PHP Rar Archiving module (php_rar) is a module that reads and decompresses rar files, but does not provide RAR compression (packaging).

1. First, go to the PECL RAR pageDownload DLL. Choose to download the corresponding version of DLL according to your own situation.

PHP version requirements: The php_rar module is suitable for php 5.2 and above, but for Windows systems, it seems that only DLL downloads corresponding to php5.3/5.4.

2. The downloaded is a zip package, and extract the two files php_rar.pdb and php_rar.dll into the ext subdirectory under the PHP installation directory.

3. Add a line of php_rar extension reference declarationextension=php_rar.dll

4. If you use the Apache server, you need to restart Apache. PHP loaded in FastCGI mode under IIS does not require further operation.

5. Write a test file to see if there is any problem

6. If there is any problem, check the server's log file.

Attached with official test code:

<?php
$archive_name = '/full/path/to/'
$entry_name = 'path/to/archive/'; //notice: no slash at the beginning
$dir_to_extract_to = '/path/to/extract/dir';
$new_entry_name = '';
$rar = rar_open($archive_name) OR die('failed to open ' . $archive_name);
$entry = rar_entry_get($rar, $entry_name) OR die('failed to find ' . $entry_name . ' in ' . $archive_name);
// this will create all necessary subdirs under $dir_to_extract_to
$entry->extract($dir_to_extract_to); 
/* OR */
// this will create only one new file $new_entry_name in $dir_to_extract_to
$entry->extract('', $dir_to_extract_to.'/'.$new_entry_name); 
// this line is really not necessary
rar_close($rar);
?>

For more information about PHP related content, please check out the topic of this site:PHP extension development tutorial》、《PHP file operation summary》、《Summary of PHP directory operation skills》、《Summary of common traversal algorithms and techniques for PHP》、《Complete collection of PHP array (Array) operation techniques》、《Introduction to PHP basic syntax》、《Summary of PHP operations and operator usage》、《Summary of usage of php strings"and"Summary of common database operation techniques for php

I hope this article will be helpful to everyone's PHP programming.