多个文件作为zip文件上传

时间:2015-01-11 09:37:01

标签: php

我有一个允许用户上传多个文件的网站。但它将多个文件上传到文件夹,现在我想压缩并存储。是否可以不使用插件?我在网上发现使用插件但是可以使用php和javascript吗?

以下是我的多文件上传代码:

$result = '';   
$zipfilename=null;   
$file = $_FILES['zipfile'];
if(empty($zipfilename))
    $zipfilename = $file['name'];
if ($file['size'] > 0) 
{
    $dir = PROBLEMZIPS."/".sprintf("%05d",$KBID)."/";
    $tmpName = $file['tmp_name'];
    $dirName = $dir;
    $fileName = $zipfilename;
    if (!is_dir($dirName)) 
    { 
        mkdir($dirName, 0777); 
    } 
    $destFile = $dirName . strtolower($fileName);
    foreach ($_FILES["zipfile"]["error"] as $key => $error) {
        if ($error == UPLOAD_ERR_OK) {
            $name = $_FILES["zipfile"]["name"][$key];
            move_uploaded_file($_FILES["zipfile"]["tmp_name"][$key], $dirName ."/". $_FILES['zipfile']['name'][$key]);       
            $zipz[] = $name;
            $result = implode(' , ', $zipz);
        }   
    }
    $changesForEveryone[] = "Zip File Uploaded: " . $result;
    $extraMsg .= "<br>Zip File Uploaded: " . $result."<br>";
} 
else 
{
    $result = false;
}

2 个答案:

答案 0 :(得分:1)

这对我有用:

<?php
$zip = new ZipArchive;
$file = Array();
$zip->open('zip/try.zip',  ZipArchive::CREATE);
foreach($_FILES['zipfile']['name'] as $file) {
echo $file;
$zip->addFile($file);
echo "<br/>";
}
$zip->close();
?>

答案 1 :(得分:0)

她是一种压缩上传文件的方法

    function zipFile($fileName,$folder,$u) {
    $zip = new ZipArchive();
    $zip->open($folder.'/'.$fileName.".rar", ZipArchive::CREATE);
    $zip->addFile($u,$fileName);
    $zip->close();
    return true;
}

$tmpFile = $_FILES["file"]["tmp_name"];
$file = $_FILES["file"]["name"];
zipFile($file,'uploads/'.$random.'',$tmpFile);
    else echo " Error while uploading";