Skip to content

Instantly share code, notes, and snippets.

@GoldenEra
Created May 5, 2015 04:24
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save GoldenEra/6ee209a75bcfa34ce600 to your computer and use it in GitHub Desktop.
Save GoldenEra/6ee209a75bcfa34ce600 to your computer and use it in GitHub Desktop.
php:extract zip and rar file
/**
* extract rar or zip file
*
* @since [1.0]
* @author Richard
* @date 2015-05-05
*
* @param [string] $file_path ["D:\file\path\a.rar"]
* @param [string] $to_path ["D:\extract\to\path"]
* @return [boolean]
*/
function extract_file($file_path, $to_path = "./")
{
$file_type = substr($file_path, strrpos($file_path, '.') - strlen($file_path) + 1);
if ("zip" === $file_type) {
$xmlZip = new ZipArchive();
if ($xmlZip->open(__DIR__."/xml.zip") === true) {
$xmlZip->extractTo($to_path);
echo "extract success";
} else {
echo "extract fail";
return false;
}
} elseif ("rar" == $file_type) {
$rar_file = rar_open($file_path) or die("Can't open Rar archive");
$entries = rar_list($rar_file);
if ($entries) {
foreach ($entries as $entry) {
echo 'Filename: ' . $entry->getName() . "\n";
$entry->extract($to_path);
}
rar_close($rar_file);
} else{
echo "extract fail";
return false;
}
}
}
@lucassch
Copy link

Thanks!

@marvinhosea
Copy link

how did u install the rar php extension?

@harryqt
Copy link

harryqt commented Apr 24, 2020

how did u install the rar php extension?

https://www.php.net/manual/en/rar.installation.php

@jiayisaidz
Copy link

cool, thx

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment