How to enable PHP RAR Archive support in XAMPP?

php rar archive In case you guys didn't know, PHP has the capability to open RAR Archives (compressed archives generated by WinRAR). RAR, by the way, stands for Roshal Archive. Named after its developer, Russian software engineer, Eugene Roshal.

While PHP is allowed to open or even extract RAR content(s), it cannot write and create RAR archives because it is not supported by the UnRAR library and is prohibited by its license. However, PHP can use exec() to run a RAR program that can do the compression.

You can find more information about RAR and UnRAR on this link: https://www.rarlabs.com.

By default, RAR Archive support is not enabled in Web Server platforms. For this tutorial, we will be tackling XAMPP. If RAR archive support is not enabled on XAMPP, whenever you use RAR-related functions, PHP would return an error that tells you there is no such function. This is actually in the form of an uncaught fatal error because not only that there is no such function, but there is also no error handler to deal with it.

The installation manual for installing RAR support can also be found in PHP Manual. Note that RAR archive requires at least PHP 5.3.0 or newer and PEAR 1.4.0 or newer in order to work.

To enable RAR archive PHP support in XAMPP, here are the following steps:

1. Download php_rar.dll

If php_rar.dll is not present in xampp/php/ext folder, then you may have to download it first from PECL. PECL is a repository for PHP Extensions.

PECL RAR repository

Select the latest stable version you can find. At the time of this writing, it's 4.2.0, but newer versions may come out in the future. Note that some versions of php_rar.dll supports only some PHP versions. For example, 4.2.0 runs only on PHP 7.2 and above. If you are running older PHP versions, look for the ones that are compatible for that version. Be sure to read the instructions on the DLLs first.

Depending on the type of CPU you are running on (either x64 or x86), pick the one that is compatible. Most desktop and laptop computers use the x86 CPU architecture. x64, on the other hand, is for 64-bit extended systems. There is also the NTS and the TS versions. NTS stands for none ZEND Thread Safety and TS stands for Thread Safety. Base your choice on your php.ini setup. If you can find zend_extension_ts= in the ini file, then download the TS version, otherwise, pick the NTS version.

2. Extract the file to your extension folder

Extract the file, and put php_rar.dll into your extension folder: \xampp\php\ext\. You don't need php_rar.pdb unless you want to compile the .dll yourself.

xampp extension folder

3. Edit your php.ini File

There are two ways to do this. You can either go via the file path xampp\php\php.ini or access it from your XAMPP console. The latter method is much faster. INI files can be edited by Notepad/Wordpad or any text editors since it's in plain text format. For this tutorial, let's go with the shortcut method. If you are already running XAMPP Control Panel, look under Apache settings in the main console, then select Config and php.ini among the options.

XAMPP Control Panel

In the php.ini file, go to Module Settings. You can find it faster if you use the Find tool and search for the keyword "module settings". Under the last extension setting on the list, add extension=php_rar.dll.

extension=php_rar

After adding, check if PHP is linked properly to PEAR's folder. Search for keyword "paths and directories". Look for include_path=. By default the include_path is \xampp\php\PEAR. If PEAR's folder is somewhere else, then change it to the path where PEAR can be found. After that, save php.ini.

xampp pear include path

4. Restart your server

Now, restart your server. If XAMPP is currently running, click the Stop button found in line with Apache on XAMPP's Control Panel. Once stopped, click Start button to start it up again. This will reboot the Apache server running on XAMPP.

To check if RAR is now enabled, use phpinfo() function. You can can create a new PHP file and use this function. If you open this file with XAMPP Apache server running, it will open PHP's info page that displays all the PHP configurations. If you installed RAR support extension properly, rar will show up under Registered PHP Streams.

phpinfo

You can also check this using XAMPP's Shell. On the command line, type php -r print_r(get_loaded_extensions()); to get the list of all loaded extensions. rar should be there in the list if you've installed it properly.

Final Notes

If RAR still fails to work, there is a chance that the .dll you are using is not compatible with the version of PHP you are using, or is not compatible with your CPU architecture. Check for these possibilities. If you are certain that you are using the correct dll, next thing to check is your php.ini file. Make sure that you added the rar extension entry under module settings, and after that, check if the PEAR include path in your php.ini is correct (see Step 3 above). If there is no problem with your php.ini settings and yet you are still having a problem with running the extension, the next step to do is to make sure that PEAR is installed in your system properly. To install PEAR, all you have to do is run pear.bat in xampp\php\ folder and it should install PEAR for you automatically. To make sure that the batch file executed flawlessly, edit pear.bat with a text editor (ie. Notepad) and add pause at the very bottom, right after @ECHO ON. This command will pause the process, and will allow you to see what the batch file executed. Without this command, the batch file will execute and close quickly once it's done with the process and you won't be able to know what it did. If pear.bat ran correctly, then there won't be any errors during the batch process. Otherwise, you may need to address the error.

Leave a Reply

Your email address will not be published. Required fields are marked *