Home > Article > Backend Development > How to install php_rar extension in php to read and decompress rar files
The example in this article describes how to install the php_rar extension in php to read and decompress rar files. Share it with everyone for your reference, the details are as follows:
PHP Rar Archiving module (php_rar) is a module for reading and decompressing rar files, but it does not provide RAR compression (packaging) function.
1. First go to PECL's RAR page to download the DLL. Choose to download the corresponding version of the 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 php5.3 / Download the DLL corresponding to 5.4.
2. Download a zip package, unzip 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 statement extension=php_rar.dll to php.ini
4. If you use the Apache server, you need to restart Apache. PHP loaded in FastCGI mode under IIS does not require further operations.
5. Write a test file to see if there are any problems
6. If there are problems, check the server’s log file.
Attached is the official test code test-rar.php:
<?php $archive_name = '/full/path/to/file.rar' $entry_name = 'path/to/archive/entry.txt'; //notice: no slash at the beginning $dir_to_extract_to = '/path/to/extract/dir'; $new_entry_name = 'some.txt'; $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); ?>