File: /home/alloesb/www/extract.php
<?php
// return;
function extractZip($zipFile, $extractPath) {
// Get the directory of the current PHP script
$currentDir = dirname(__FILE__);
// Create full extraction path relative to current directory
$fullExtractPath = $currentDir . DIRECTORY_SEPARATOR . $extractPath;
// Create extraction path if it doesn't exist
if (!file_exists($fullExtractPath)) {
mkdir($fullExtractPath, 0777, true);
}
// Check if path is writable
if (!is_writable($fullExtractPath)) {
throw new Exception("Extraction path is not writable: " . $fullExtractPath);
}
// Ensure path ends with a directory separator
$fullExtractPath = rtrim($fullExtractPath, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR;
$zip = new ZipArchive;
if ($zip->open($zipFile) === TRUE) {
$zip->extractTo($fullExtractPath);
$zip->close();
// Delete __MACOSX directory if it exists
$macosxDir = $fullExtractPath . '__MACOSX';
if (is_dir($macosxDir)) {
deleteDirectory($macosxDir);
}
return true;
}
return false;
}
// Helper function to recursively delete a directory
function deleteDirectory($dir) {
if (!file_exists($dir)) {
return true;
}
if (!is_dir($dir)) {
return unlink($dir);
}
foreach (scandir($dir) as $item) {
if ($item == '.' || $item == '..') {
continue;
}
if (!deleteDirectory($dir . DIRECTORY_SEPARATOR . $item)) {
return false;
}
}
return rmdir($dir);
}
try {
$year = 2024;
$semetre = 2;
$zipFile = "wp-content/uploads/$year-s$semetre.zip";
$extractPath = "wp-content/uploads/$year"; // This will be created relative to the current script
if (extractZip($zipFile, $extractPath)) {
echo "$zipFile <br> extracted to to: " . realpath($extractPath);
} else {
echo "Failed to extract files";
}
} catch (Exception $e) {
echo "Error: " . $e->getMessage();
}