Search code examples
phparchiverar

PHP - Extracting all files of rar archive to main folder


I have a rar file with the many files and folders. I want to extract files in the sub-folders of the rar file to the main folder.

I have tried this:

$archive = RarArchive::open('example.rar');

$entries = $archive->getEntries();

foreach ($entries as $entry)
    $entry->extract($dir);

$archive->close();

However this extracts the files to the same folder, rather than the main folder.

Any suggestions?


Solution

  • i tried an own solution, and it's works:

    $archive = RarArchive::open('example.rar');
    
    $entries = $archive->getEntries();
    
    foreach ($entries as $entry)
    {
            $fileinfo = pathinfo($entry->getName());
            copy("rar://".$file."#".$entry->getName(), $dir.'/'.$fileinfo['basename']);
    }
    
    $archive->close();
    

    for don't extract folders (empty folders), we can put

    if(!empty($fileinfo['extension']))
    

    before copy function.

    thanks to me :-)