function upload_save($redirect = true)
 {
     global $my, $mainframe, $database, $option, $priTask, $subTask;
     global $WBG_CONFIG, $wbGalleryDB_cat, $wbGallery_common, $wbGallery_eng;
     // Prepare Runtime
     $tempDir = null;
     $known_images = array('image/pjpeg', 'image/jpeg', 'image/jpg', 'image/png', 'image/gif');
     $time = time();
     // Importing
     $importFolder = mosGetParam($_REQUEST, 'folder', '');
     if ($importFolder && !file_exists($importFolder)) {
         echo "<script> alert('Import Folder Does Not Exist'); document.location.href='index2.php?option=" . $option . "&task=image.upload'; </script>\n";
         exit;
     }
     // Default Values
     $defRow = new wbGalleryDB_img($database);
     $defRow->bind($_POST);
     // Debug
     echo "Image Processing Start: " . $time . '<br/>';
     // ==============================v========================================
     // Single File Upload
     if (!empty($_FILES['img']['tmp_name'])) {
         // Debug
         echo "Single File Detected <br/>";
         if (!in_array($_FILES['img']['type'], $known_images)) {
             echo "<script> alert('Image type: " . $_FILES['img']['type'] . " is an unknown type'); document.location.href='index2.php?option=" . $option . "&task=image.upload'; </script>\n";
             exit;
         }
         $wbGallery_eng->add($_FILES['img']['tmp_name'], $_FILES['img']['name'], $_FILES['img']['type'], $defRow);
         if ($redirect) {
             mosRedirect('index2.php?option=' . $option . '&task=image.upload', 'Image Saved');
         }
     }
     // ==============================v========================================
     // Zip File Upload
     if (!empty($_FILES['zip']['tmp_name'])) {
         //zip file upload
         // Debug
         echo "Compressed File Uploaded <br/>";
         // Create / Define Temporary Folder for Unzipped Files
         if (!mkdir($mainframe->getCfg('absolute_path') . '/media/' . $time)) {
             if (!mkdir('/tmp/' . $time)) {
                 echo "<script> alert('Unable to Create Temp Directory'); history.back(); </script>\n";
                 exit;
             } else {
                 $tempDir = '/tmp/' . $time;
             }
         } else {
             $tempDir = $mainframe->getCfg('absolute_path') . '/media/' . $time;
         }
         // Uncompress ZIP or TAR.GZ
         if (preg_match('/zip$/i', $_FILES['zip']['name'])) {
             // Load ZIP functions
             require_once $mainframe->getCfg('absolute_path') . '/administrator/includes/pcl/pclzip.lib.php';
             require_once $mainframe->getCfg('absolute_path') . '/administrator/includes/pcl/pclerror.lib.php';
             $zipfile = new PclZip($_FILES['zip']['tmp_name']);
             if (substr(PHP_OS, 0, 3) == 'WIN') {
                 define('OS_WINDOWS', 1);
             } else {
                 define('OS_WINDOWS', 0);
             }
             $ret = $zipfile->extract(PCLZIP_OPT_PATH, $tempDir);
             if ($ret == 0) {
                 $wbGallery_common->remove_dir($tempDir);
                 echo "<script> alert('ZIP Extraction Error: " . $zipfile->errorName(true) . "'); history.back(); </script>\n";
                 exit;
             }
         } elseif (preg_match('/tar.gz$/i', $_FILES['zip']['name'])) {
             // Load TAR functions
             require_once $mainframe->getCfg('absolute_path') . '/includes/Archive/Tar.php';
             $archive = new Archive_Tar($_FILES['zip']['tmp_name']);
             $archive->setErrorHandling(PEAR_ERROR_PRINT);
             if (!$archive->extractModify($tempDir, '')) {
                 $wbGallery_common->remove_dir($tempDir);
                 echo "<script> alert('TAR Extraction Error'); history.back(); </script>\n";
                 exit;
             }
         } else {
             // Unknown File...
             $wbGallery_common->remove_dir($tempDir);
             echo "<script> alert('Unknown File Format - Must be .ZIP or .TAR.GZ'); history.back(); </script>\n";
             exit;
         }
     }
     // Zip File Upload
     // ==============================v========================================
     // Process Files from Folder
     if ($tempDir || $importFolder) {
         $processDirs = array();
         $files_added = 0;
         $files_skipped = 0;
         if ($tempDir) {
             $processDirs[] = array('path' => $tempDir, 'remove' => 1);
         }
         if ($importFolder) {
             $processDirs[] = array('path' => $importFolder, 'remove' => 0);
         }
         if (count($processDirs)) {
             foreach ($processDirs as $procDir) {
                 // Read Files from Temp Folder
                 $regImg = array();
                 foreach ($known_images as $k) {
                     $regImg[] = preg_replace('/^.*\\//', '', $k) . '$';
                 }
                 $regStr = '/' . join('|', $regImg) . '/';
                 $files = $wbGallery_common->find_files($procDir['path'], $regStr);
                 unset($regImg);
                 unset($regStr);
                 // Debug
                 echo "Unzipped " . count($files) . " for processing <br/>";
                 if (count($files)) {
                     foreach ($files as $file) {
                         $filePath = $file['path'] . '/' . $file['name'];
                         $res = getimagesize($filePath);
                         $fileType = $res['mime'];
                         if (in_array($fileType, $known_images)) {
                             if ($wbGallery_eng->add($filePath, $file['name'], $fileType, $defRow)) {
                                 $files_added++;
                             } else {
                                 $files_skipped++;
                             }
                         } else {
                             $files_skipped++;
                         }
                     }
                 }
                 if ($procDir['remove']) {
                     $wbGallery_common->remove_dir($procDir['path']);
                 }
             }
             // foreach processDirs
         }
         // if processDirs
         if ($redirect) {
             if (!$files_added && !$files_skipped) {
                 mosRedirect('index2.php?option=' . $option . '&task=image.upload', 'Error: No Files were Processed or Error Finding Files');
             } else {
                 mosRedirect('index2.php?option=' . $option . '&task=image.upload', $files_added . ' images added - ' . $files_skipped . ' files skipped');
             }
         } else {
             if (!$files_added && !$files_skipped) {
                 return false;
             } else {
                 return true;
             }
         }
     }
     mosRedirect('index2.php?option=' . $option . '&task=image.upload', 'Please Specify Images for Processing');
     return false;
 }