1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

[PHP] Extracting .Rar File

Discussion in 'PHP' started by NetworkTown.Net, May 29, 2007.

  1. #1
    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
     
    NetworkTown.Net, May 29, 2007 IP
  2. Servii

    Servii Banned

    Messages:
    55
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #2
    <?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
     
    Servii, May 29, 2007 IP
    NetworkTown.Net likes this.
  3. gibex

    gibex Active Member

    Messages:
    1,060
    Likes Received:
    21
    Best Answers:
    0
    Trophy Points:
    95
    #3
    gibex, May 29, 2007 IP
    NetworkTown.Net likes this.
  4. NetworkTown.Net

    NetworkTown.Net Well-Known Member

    Messages:
    2,022
    Likes Received:
    28
    Best Answers:
    0
    Trophy Points:
    165
    #4
    Well about the PECL i need someone to explain how to install it and then get it working. Rep added to both of you.
     
    NetworkTown.Net, May 29, 2007 IP
  5. krakjoe

    krakjoe Well-Known Member

    Messages:
    1,795
    Likes Received:
    141
    Best Answers:
    0
    Trophy Points:
    135
    #5
    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
     
    krakjoe, May 29, 2007 IP
  6. NetworkTown.Net

    NetworkTown.Net Well-Known Member

    Messages:
    2,022
    Likes Received:
    28
    Best Answers:
    0
    Trophy Points:
    165
    #6
    Right i got that done but for the php file

     
    NetworkTown.Net, May 29, 2007 IP
  7. krakjoe

    krakjoe Well-Known Member

    Messages:
    1,795
    Likes Received:
    141
    Best Answers:
    0
    Trophy Points:
    135
    #7
    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 ......
     
    krakjoe, May 29, 2007 IP
    NetworkTown.Net likes this.
  8. NetworkTown.Net

    NetworkTown.Net Well-Known Member

    Messages:
    2,022
    Likes Received:
    28
    Best Answers:
    0
    Trophy Points:
    165
    #8
    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
     
    NetworkTown.Net, May 29, 2007 IP
  9. krakjoe

    krakjoe Well-Known Member

    Messages:
    1,795
    Likes Received:
    141
    Best Answers:
    0
    Trophy Points:
    135
    #9
    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
     
    krakjoe, May 30, 2007 IP