GOPI GOPI
GOPI GOPI

Reputation: 1

How to extract rar/archive files and folders using php in windows?

I like extract the rar/archive files and folder using php. But i have tried using this script

<?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"; 
        $entry->extract('/dir/extract/to/'); 
    } 
    rar_close($rar_file); 
?>

Upvotes: 0

Views: 6809

Answers (2)

Guilherme Freire
Guilherme Freire

Reputation: 372

$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);

Upvotes: 0

Jan Schejbal
Jan Schejbal

Reputation: 4033

The function you are using requires that you install it first.

Instead of it, you may want to call a command-line tool using exec(), but make sure you protect yourself from shell injection.

Upvotes: 1

Related Questions