-
-
Save locvfx/2c5cb064c8df1b607f70eb4c3152a855 to your computer and use it in GitHub Desktop.
php:extract zip and rar file
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Install rar Extension
https://www.php.net/manual/en/rar.installation.php