Example #1
0
function create_zip_archive($files = array(), $destination = '', $overwrite = false)
{
    //If the zip file already exists and overwrite is false return false
    if (file_exists($destination) && !$destination) {
        return false;
    }
    $valid_files = array();
    //If files were passed in...
    if (is_array($files)) {
        //cycle through each file
        foreach ($files as $file) {
            //Make sure the file exists
            if (file_exists($file)) {
                $valid_files[] = $file;
            }
        }
    }
    //If we have good files...
    if (count($valid_files)) {
        //create the archive
        $zip = new zipArchive();
        if ($zip->open($destination, $overwrite ? ZIPARCHIVE::OVERWRITE : ZIPARCHIVE::CREATE) != true) {
            return false;
        }
        //Add the files
        foreach ($valid_files as $file) {
            $zip->addFile($file, $file);
        }
        //debug
        //echo 'The zip archive contains',$zip->numFiles,' files with a status of ',$zip->status;
        //Close the zip -- done!
        $zip->close();
        //Check to make sure the file exists
        return file_exists($destination);
    } else {
        return false;
    }
}
Example #2
0
 public function mdown()
 {
     if (!extension_loaded('zip')) {
         include VIEW_PATH . 'error.php';
         exit;
     }
     if (extension_loaded('fastdfs_client')) {
         include VIEW_PATH . 'error.php';
         exit;
     }
     set_time_limit(2 * 3600);
     $fac = Factory::getInstance();
     $ids = $_REQUEST['ids'];
     $info = $fac->getFileMap($ids);
     if (is_numeric($ids)) {
         $tmp = $info;
         unset($info);
         $info[0] = $tmp;
     }
     if ($info) {
         foreach ($info as $v) {
             //
             if ($_SESSION['CLOUD_UID'] != $v['uid']) {
                 $pids = str_replace('/', ',', trim($v['path'], '/'));
                 $ids = ($pids ? $pids . ',' : '') . $v['id'];
                 $shares = $fac->getShareByMap($ids);
                 if ($shares) {
                     $keys = array();
                     foreach ($shares as $y) {
                         $keys[$y['mapId']] = $y['mapId'];
                         $shares[$y['mapId']] = $y;
                     }
                     $shareInfo = $shares[max($keys)];
                 } else {
                     continue;
                 }
                 if ($shareInfo) {
                     if (strtotime($shareInfo['overTime']) < time() && $shareInfo['overTime'] != '0000-00-00 00:00:00') {
                         continue;
                     }
                     $fac->incrShareDown($shareInfo['id']);
                     if ($shareInfo['pwd'] && !$_SESSION['share'][self::getClientIp() . ':' . $shareInfo['mapId']]) {
                         continue;
                     }
                 } else {
                     continue;
                 }
             }
             //
             if ($v['isdir']) {
                 $list = $fac->getFileMapByPid($v['uid'], $v['id'], 1);
                 if ($list) {
                     foreach ($list as $y) {
                         $dirs[] = $y['location'];
                     }
                 }
             } else {
                 $dirs[] = $v['location'];
             }
         }
     } else {
         include VIEW_PATH . 'error.php';
         exit;
     }
     $zip = new zipArchive();
     $zipName = md5(uniqid() . time()) . '.zip';
     $zip->open($zipName, ZIPARCHIVE::OVERWRITE);
     foreach ($dirs as $v) {
         $zip->addFile($v, basename($v));
     }
     $zip->close();
     header('Content-Type:Application/zip');
     if (preg_match("/MSIE/", $_SERVER["HTTP_USER_AGENT"])) {
         header('Content-Disposition: attachment; filename="' . rawurlencode($zipName) . '"');
     } else {
         if (preg_match("/Firefox/", $_SERVER["HTTP_USER_AGENT"])) {
             header('Content-Disposition: attachment; filename*="utf8\'\'' . $zipName . '"');
         } else {
             header('Content-Disposition: attachment; filename="' . $zipName . '"');
         }
     }
     header('Content-Length:' . filesize($zipName));
     session_write_close();
     readfile($zipName);
     ob_flush();
     flush();
     unlink($zipName);
 }
    } else {
        $sql_document_root = "select tvalue from config where NAME='DOWNLOAD_PACK_DIR'";
    }
    $res_document_root = mysqli_query($_SESSION['OCS']["readServer"], $sql_document_root);
    while ($val_document_root = mysqli_fetch_array($res_document_root)) {
        $document_root = $val_document_root["tvalue"] . '/download/';
    }
    //echo $document_root;
    //if no directory in base, take $_SERVER["DOCUMENT_ROOT"]
    if (!isset($document_root)) {
        $document_root = VARLIB_DIR . '/download/';
        if ($protectedGet['type'] == "server") {
            $document_root .= "server/";
        }
    }
    $rep = $document_root . $protectedGet["timestamp"] . "/";
    $dir = opendir($rep);
    $tmpfile = tempnam("/tmp", ".zip");
    $zipfile->open($tmpfile, ZipArchive::CREATE);
    while ($f = readdir($dir)) {
        if (is_file($rep . $f)) {
            $zipfile->addFile($rep . $f, $f);
        }
    }
    $zipfile->close();
    closedir($dir);
    readfile($tmpfile);
    unlink($tmpfile);
    exit;
    exit;
}