Skip to content

Instantly share code, notes, and snippets.

@locvfx
Forked from GoldenEra/functions.php
Last active August 25, 2023 03:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save locvfx/2c5cb064c8df1b607f70eb4c3152a855 to your computer and use it in GitHub Desktop.
Save locvfx/2c5cb064c8df1b607f70eb4c3152a855 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;
}
}
}
@locvfx
Copy link
Author

locvfx commented Aug 24, 2023

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