Online Resources

"; echo "
" . end(explode('/',$dir)) . "
"; echo "
"; echo "
"; } function displayFile($path){ $file = end(explode('/',$path)); echo "
"; echo "
" . $file . "
"; echo '
'; echo "
"; } // Create zip function createZip($zip,$dir,$cutPath = ''){ if ($cutPath == ''){ $cutPath = substr($dir, 0, strpos($dir, end(explode('/',$dir))) - 1); $shortPath = substr(substr($dir, strlen($cutPath)), 1); var_dump($shortPath); $zip->addEmptyDir($dir, $shortPath); $folder = $dir; // Read data of the folder createZip($zip, $folder, $cutPath); //var_dump($cutPath, $dir); } else { if (is_dir($dir)) { if ($dh = opendir($dir)) { while (($file = readdir($dh)) !== false) { $path = $dir . '/' . $file; // If file if (!is_dir($path)) { //var_dump('file ' . $path); if ($file != '' && $file != '.' && $file != '..') { $shortPath = substr(substr($path, strlen($cutPath)), 1); var_dump($path, $shortPath); //substr($path,$pos + strlen($file) + 1) $zip->addFile($path, $shortPath); } } else { // If directory if (is_dir($path)) { if ($file != '' && $file != '.' && $file != '..') { //var_dump('directory ' . $path); // Add empty directory $shortPath = substr(substr($path, strlen($cutPath)), 1); $zip->addEmptyDir($path, $shortPath); $folder = $path; // Read data of the folder createZip($zip, $folder, $cutPath); } } } } closedir($dh); } } } } function downloadZip($pathdir){ $previousZips = glob('./*.zip'); foreach ($previousZips as $zipFile){ unlink($zipFile); } // Enter the name to creating zipped directory $filename = end(explode('/',$pathdir)) . ".zip"; //$filename = $pathdir . ".zip"; $zip = new ZipArchive(); if ($zip->open($filename, ZipArchive::CREATE)!==TRUE) { exit("cannot open <$filename>\n"); } $dir = $pathdir; // Create zip createZip($zip,$dir); $res = $zip->close(); var_dump($zip->getStatusString()); if (file_exists($filename)) { $handle = fopen($filename, "r"); header("Location: https://resources.smp.org/" . $filename); //unlink($filename); } } // Define a function to output files in a directory function outputFiles($path){ echo "
"; echo "" . end(explode('/',substr_replace($path, 'Courses', 0, 22))) . ""; echo "
"; if($path != './Kno/Customer_Courses' && $path != './Kno/Customer_Courses/' && strlen($path) >= 22){ echo "
"; echo "
Back
"; echo "
"; } $dir_array = []; // Check directory exists or not if(file_exists($path) && is_dir($path)){ // Scan the files in this directory $result = scandir($path); // Filter out the current (.) and parent (..) directories $files = array_diff($result, array('.', '..')); if(count($files) > 0){ // Loop through returned array foreach($files as $file){ if ($file[0] != '.' && substr($file, -4) != ".zip"){ if(is_file("$path/$file")){ // Display filename displayFile("$path/$file"); } else if(is_dir("$path/$file")){ // Recursively call the function if directories found //outputFiles("$path/$file"); $dir_array[] = "$path/$file"; displayDir("$path/$file"); } } } } else{ echo "ERROR: No files found in" . $path; } } else { echo "ERROR: The directory does not exist."; } foreach ($dir_array as $dir){ if($_GET['opendir'] == $dir){ sendCode($dir); } } } // Call the function if(isset($_GET['opendir'])){ $path = $_GET['opendir']; if (strlen($path) < 22) $path = './Kno/Customer_Courses'; ob_start(); outputFiles($path); } else { ob_start(); outputFiles("./Kno/Customer_Courses"); } if(isset($_GET['zipdir'])){ downloadZip($_GET['zipdir']); } ?>