Created
November 14, 2019 22:23
-
-
Save Alexander-Pop/3ee243e162e78f55e0ef147f1150dedc to your computer and use it in GitHub Desktop.
PHP - extract only images from a zip file #php #images #compress
This file contains hidden or 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
<?php | |
/* | |
* this script will extract only images from a zip file and skip all other files. | |
*/ | |
error_reporting(E_ALL); | |
ini_set('display_errors', '1'); | |
/** | |
* Makes directory and returns BOOL(TRUE) if exists OR made. | |
* | |
* @param $path Path name | |
* @return bool | |
*/ | |
function rmkdir($path, $mode = 0755) { | |
$path = rtrim(preg_replace(array("/\\\\/", "/\/{2,}/"), "/", $path), "/"); | |
$e = explode("/", ltrim($path, "/")); | |
if (substr($path, 0, 1) == "/") { | |
$e[0] = "/" . $e[0]; | |
} | |
$c = count($e); | |
$cp = $e[0]; | |
for ($i = 1; $i < $c; $i++) { | |
if (!is_dir($cp) && !@mkdir($cp, $mode)) { | |
return false; | |
} | |
$cp .= "/" . $e[$i]; | |
} | |
return @mkdir($path, $mode); | |
} | |
$dir = __DIR__; | |
$extractedFilesDir = $dir . "/images"; | |
$zipFiles = glob($dir . "/*.zip"); | |
if (is_array($zipFiles) && count($zipFiles) > 0) { | |
foreach ($zipFiles as $zipFile) { | |
if (file_exists($zipFile)) { | |
$zipFolderName = basename($zipFile); | |
$zipFileName = basename($zipFile, '.zip'); | |
$fieDir = $extractedFilesDir . '/' . $zipFileName; | |
rmkdir($extractedFilesDir); | |
if (class_exists('ZipArchive')) { | |
$zip = new ZipArchive; | |
$result = $zip->open($zipFile, ZipArchive::CHECKCONS); | |
if ($result === TRUE) { | |
$noImageFound = true; | |
for ($i = 0; $i < $zip->numFiles; $i++) { | |
$onlyFileName = $zip->getNameIndex($i); | |
$fileType = mime_content_type('zip://' . $zipFile . '#' . $onlyFileName); | |
$fileType = strtolower($fileType); | |
if (($fileType == 'image/png' || $fileType == 'image/jpeg' || $fileType == 'image/gif' || $fileType == 'image/svg') && (preg_match('#\.(SVG|svg|jpg|jpeg|JPEG|JPG|gif|GIF|png|PNG)$#i', $onlyFileName))) { | |
//copy('zip://' . $zipFile . '#' . $onlyFileName, $fieDir . '/' . $onlyFileName); | |
$zip->extractTo($extractedFilesDir, array($zip->getNameIndex($i))); | |
$noImageFound = false; | |
} | |
} | |
if ($noImageFound) { | |
echo 'There is no images in zip file ' . $zipFolderName . '<br />'; | |
} | |
} else { | |
switch ($result) { | |
case ZipArchive::ER_NOZIP: | |
echo 'Not a zip archive ' . $zipFolderName; | |
case ZipArchive::ER_INCONS: | |
echo 'Consistency check failed ' . $zipFolderName; | |
case ZipArchive::ER_CRC: | |
echo 'checksum failed ' . $zipFolderName; | |
default: | |
echo 'Error occured while extracting file ' . $zipFolderName; | |
} | |
} | |
$zip->close(); | |
} else { | |
echo 'Zip Library is not installed in your server, please installed it to process zip files' . '<br />'; | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks Dude 👍