【问题标题】:I am not able to create and store a zip file using php我无法使用 php 创建和存储 zip 文件
【发布时间】:2015-07-09 10:44:04
【问题描述】:

我正在尝试创建一个代码,该代码通过文件上传控制从本地计算机中选择一个 csv 文件。 csv 文件包含我要从服务器下载的文件的名称。当我按下提交按钮时,代码会读取 csv 文件并提取必须下载的文件的名称。现在代码应该创建一个 zip 文件并仅将那些文件添加到 csv 文件中提到的 zip 文件中。为此,我创建了以下代码:

<?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>

当语句 print_r($zip); 时,我正在获取添加到 zip 文件中的 csv 文件名称已执行,但我找不到在服务器文件夹中创建的文件。所以我无法下载文件。应该做什么才能将 csv 文件中的文件名添加到 zip 文件中,然后将其下载到本地 PC。请帮我找出我错在哪里。谢谢。

【问题讨论】:

  • 创建zip文件的文件夹的权限是什么..?
  • 在这里你删除文件unlink($destination) 然后不久之后,你尝试返回一个布尔值,如果它存在,它将永远不存在return file_exists($destination);
  • 在调用 zip 文件头之前,您已经输出到浏览器,在代码中与 zip 文件无关的这些头之后也有输出。这行不通。
  • ^ 它应该提供一个 zip 文件作为下载文件,但由于 echo 导致其中的输出无效,该 zip 文件会损坏

标签: php


【解决方案1】:

这一行

echo "hiii";

将阻止 header() 发送,因为如果新加载的页面上有内容,您将无法发送标题。此外,如果您在之后添加内容,那么您的 zip 将会损坏。尝试前后不打印任何内容,它会起作用。

哦,正如 pokeybit 所指出的,您正在取消链接文件然后验证它是否存在。这是没有意义的。此外,如果你没有通过

    if(count($valid_files)) {

, $zip->close();不会工作,因为 $zip 没有被声明。

【讨论】:

    【解决方案2】:

    感谢 dlegall pokeybit 和所有其他试图帮助我解决问题的人。我对代码做了很多修改,最后对我有用的代码如下:

    <?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>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-08-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-11-10
      • 1970-01-01
      • 2021-04-26
      相关资源
      最近更新 更多