Hi Anyone know the php code to extract a .RAR file? I know it can be done but dont know the command for it. Please post it here if you know, Ill add a Green Rep for you. Thanks
<?php $rar_file = rar_open('example.rar') or die("Can't open Rar archive"); $entries = rar_list($rar_file); foreach ($entries as $entry) { echo 'Filename: ' . $entry->getName() . "\n"; echo 'Packed size: ' . $entry->getPackedSize() . "\n"; echo 'Unpacked size: ' . $entry->getUnpackedSize() . "\n"; $entry->extract('/dir/extract/to/'); } rar_close($rar_file); ?> Thanks
first you need PECL rar from http://pecl.php.net/package/rar, then you can try that example code from http://php.net/rar
Well about the PECL i need someone to explain how to install it and then get it working. Rep added to both of you.
Building php extensions in unix is pretty much the same across the board, they depend on three things, a working build environment ( automake, autoconf, gcc, etc ), your php versions "phpize" binary, and the include headers installed with php. wget http://pecl.php.net/get/rar mv rar rar.tgz // most don't need this, theres a bug with it I think tar xzf rar.tgz cd rar* phpize ./configure && make make install you should add the extension ( its location will be the last line of output from make install ) to php.ini, or copy it to the webspace you want to load it @ and dl( "php_module.so" ) should do the trick, if you add it to php.ini ( the system one ) then you must restart apache for the file to be parsed and the extension loaded, if you just copy you don't need to restart apache. You need to be root when you do make install, and on some systems you need root to use the build tools, so phpize might fail in this case if you aren't root, its a general ( and pretty good ) rule on unix that normal users cannot install binaries into system folders, so be root. done
a relative path should work, if you only know the relative path then $entry->extract( realpath( 'archives/name' ) ); PHP: I'd use the full one if I could ......
ok and one last this do i have to put name.txt ect at the end becuase dosent it extract the files. Edit: for some reason the file dont extract this is the code: <?php $rar_file = rar_open('sendto.rar') or die("Can't open Rar archive"); $entries = rar_list($rar_file); foreach ($entries as $entry) { echo 'Filename: ' . $entry->getName() . "\n"; echo 'Packed size: ' . $entry->getPackedSize() . "\n"; echo 'Unpacked size: ' . $entry->getUnpackedSize() . "\n"; $entry->extract(realpath('imgz/file.txt')); } rar_close($rar_file); ?> PHP: and when i run the file i get
You need to extract to a path, when you loop over an archive the file handles will be in locations similar to /folder1/folder2/file1.ext so the path you are passing to extract() should be something like /home/me/www/archives or archives/ then the files will be extracted to /home/me/www/archives/folder1/folder2/file1.ext or archives/folder1/folder2/file1.ext