drtsd7864 2015-07-09 10:44
浏览 40
已采纳

我无法使用php创建和存储zip文件

I am trying to create a code which selects a csv file from the local computer via file upload control. The csv file contains the names of the files I want to download from the server. When I press the submit button the code reads the csv file and extracts the names of the files which has to be downloaded. Now the code should create a zip file and add only those files to zip file which are mentioned in the csv file. For that I have created the following code:

<?php
if(isset($_POST['submit'])){
$filename=$_FILES['filename'];
$handle = fopen($_FILES['filename']['tmp_name'],"r");
$i=0;
  while(($data = fgetcsv($handle,1000,","))!== FALSE){
  $filenum[$i]=$data[0];
  $i++;
}
function create_zip($files = array(),$destination = '',$overwrite = false) {
    //if the zip file already exists and overwrite is false, return false

    if(file_exists($destination) && !$overwrite) { return false; }
    //vars

    $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("uploadedfiles/".$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);
        }
        print_r($zip);
        //debug
        echo 'The zip archive contains ',$zip->numFiles,' files with a status of ',$zip->status;
        if (file_exists($destination) ){
        // push to download the zip
        echo "hiii";
        header('Content-type: application/zip');
        header('Content-Disposition: attachment; filename="' . $destination . '"');
        readfile($destination);
        // remove zip file is exists in temp path
        unlink($destination);
    } else {
        echo "error";
    }

        //close the zip -- done!
        $zip->close();
        //check to make sure the file exists
        return file_exists($destination);
    }
    else
    {
        return false;
    }
}
$result = create_zip($filenum,$_SERVER['DOCUMENT_ROOT'].'/misc/my-archive.zip');
}
?>
<form enctype='multipart/form-data' action='fileuploadproject.php' method='post'>
<input size='50' type='file' name='filename' id='filename'><br>
<input type='submit' name='submit' value='Download'>
</form>

I am getting the names of the csv files names added to the zip file when the statement print_r($zip); is executed but I cannot find the file created in the server folder. And so I cannot download the files. What should be done inorder to get the filenames in the csv file get added to the zip file and then download it to local pc. Please help me find where am I mistaken. Thanks.

  • 写回答

2条回答 默认 最新

  • doutinghou6980 2015-07-10 15:19
    关注

    Thanks to dlegall pokeybit and all others who tried to help me solve the problem. I have done many changes in the code and finally the code which worked for me is given below:

    <?php
    if(isset($_POST['submit'])){
    $filename=$_FILES['filename'];
    $handle = fopen($_FILES['filename']['tmp_name'],"r");
    $i=0;
      while(($data = fgetcsv($handle,1000,","))!== FALSE){
      $filenum[$i]=$data[0];
      $i++;
      }
    
        $valid_files = array();
        //if files were passed in...
        if(is_array($filenum)) {
            //cycle through each file
            foreach($filenum as $file) {
                //make sure the file exists
                if(file_exists("uploadedfiles/".$file)) {
                    $valid_files[] = $file;
                }
            }
     }
    
        //if we have good files...
    if(count($valid_files)) {
            //create the archive
            $zip = new ZipArchive;
    if ($zip->open("uploadedfiles/archive.zip",  ZipArchive::CREATE)) {
                foreach($valid_files as $file) {
                $zip->addFile("uploadedfiles/".$file,$file);
            }
            $zip->close();
    }
    else
    {
            echo "Not Created";
    }
    
            if (file_exists("uploadedfiles/archive.zip") ){
            // push to download the zip
            $archive_file_name="uploadedfiles/archive.zip";
            $archive_file="archive.zip";
            header("Content-type: application/zip"); 
            header("Content-Disposition: attachment; filename=$archive_file"); 
            header("Pragma: no-cache"); 
            header("Expires: 0"); 
            readfile("$archive_file_name");
            unlink($archive_file_name);
            exit;
        } else {
            echo "There was some problem in downloading the file";
        }
    }
    else{
     echo "<script>alert('No files in the CSV match the files on the server');</script>";
    }
    
    } 
    ?>
    
    <form enctype='multipart/form-data' action='fileuploadproject.php' method='post'>
    Select the CSV file: <input style="color:white;background:coral;font-size:16px" size='50' type='file' name='filename' id='filename' required>
    <input style="color:white;background:green;font-size:20px" type='submit' name='submit' value='Download'>
    </form>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥50 易语言把MYSQL数据库中的数据添加至组合框
  • ¥20 求数据集和代码#有偿答复
  • ¥15 关于下拉菜单选项关联的问题
  • ¥20 java-OJ-健康体检
  • ¥15 rs485的上拉下拉,不会对a-b<-200mv有影响吗,就是接受时,对判断逻辑0有影响吗
  • ¥15 使用phpstudy在云服务器上搭建个人网站
  • ¥15 应该如何判断含间隙的曲柄摇杆机构,轴与轴承是否发生了碰撞?
  • ¥15 vue3+express部署到nginx
  • ¥20 搭建pt1000三线制高精度测温电路
  • ¥15 使用Jdk8自带的算法,和Jdk11自带的加密结果会一样吗,不一样的话有什么解决方案,Jdk不能升级的情况