php-ziparchive-readfile,没有发现这样的文件错误


php ziparchive readfile, no such file found error

我遇到这个错误:

【2013年7月23日16:26:15美国/纽约】PHP警告:readfile(Resumes.zip):无法打开流:没有这样的文件或第24行/home/mcaplace/public_html/download.php中的目录

这是代码:

<?php
    /*foreach($_POST['files'] as $check) {
        echo $check;}*/
    function zipFilesAndDownload($file_names,$archive_file_name,$file_path)
    {
        $zip = new ZipArchive();
        //create the file and throw the error if unsuccessful
        if ($zip->open($archive_file_name, ZIPARCHIVE::CREATE )!==TRUE) {
        exit("cannot open <$archive_file_name>'n");
        }
        //add each files of $file_name array to archive
        foreach($file_names as $files)
        {
        $zip->addFile($file_path.$files,$files);
        /*if(file_exists($file_path.$files)) //." ".$files."<br>";
            echo "yes";*/
        }
        $zip->close();
    //then send the headers to foce download the zip file
        header("Content-type: application/zip");
        header("Content-Disposition: attachment; filename=$archive_file_name");
        header("Pragma: no-cache");
        header("Expires: 0");
        readfile($archive_file_name);
        exit;
    }
    //If you are passing the file names to thae array directly use the following method
    $file_names = $_POST['files'];
    //if you are getting the file name from database means use the following method
    //Include DB connection
    $archive_file_name='Resumes.zip';
    $file_path=$_SERVER['DOCUMENT_ROOT']."/resumes/";
    zipFilesAndDownload($file_names,$archive_file_name,$file_path); 
 ?>

您有几处错误首先,ZIPARCHIVE::CREATE不正确,它应该是ZIPARCHIVE::CREATE或ZIPARCHIVE::OVERWRITE(如果您没有删除zip文件,则覆盖)。

接下来,你的路径是错误的。您并没有告诉readfile()文件的绝对路径。因此,您需要为zip文件的保存位置和读取位置提供绝对路径。

此外,我发现你的根文件夹的网站是不可写的。所以我把下面的代码压缩到简历文件夹中。我倾向于远离$_SERVER['DOCUMENT_ROOT'],使用realpath()函数。

最后,你必须确保zip文件被创建到哪个文件夹,它有777权限

这是完整的代码更改

<?php
    /*foreach($_POST['files'] as $check) {
        echo $check;}*/
    function zipFilesAndDownload($file_names,$archive_file_name,$file_path)
    {
        $zip = new ZipArchive();
        //create the file and throw the error if unsuccessful
        if ($zip->open($file_path.$archive_file_name, ZipArchive::OVERWRITE )!==TRUE) {
            exit("cannot open <$archive_file_name>'n");
        }
        //add each files of $file_name array to archive
        foreach($file_names as $files)
        {
            $zip->addFile($file_path.$files,$files);
            /*if(file_exists($file_path.$files)) //." ".$files."<br>";
                echo "yes";*/
        }
        $zip->close();
    //then send the headers to foce download the zip file
        header("Content-type: application/zip");
        header("Content-Disposition: attachment; filename=$archive_file_name");
        header("Pragma: no-cache");
        header("Expires: 0");
        readfile($file_path.$archive_file_name);
        exit;
    }
    //If you are passing the file names to thae array directly use the following method
    $file_names = array('test.txt');
    //if you are getting the file name from database means use the following method
    //Include DB connection
    $archive_file_name='Resumes.zip';
    $file_path=$_SERVER['DOCUMENT_ROOT']."/resumes/";
    zipFilesAndDownload($file_names,$archive_file_name,$file_path); 
 ?>

我在PHP 7.0中遇到了类似的问题,可以使用OHCC的解决方案来解决:http://php.net/manual/de/ziparchive.open.php#118273

他建议同时使用OVERWRITE和CREATE这两个标志。

$zip->open('i.zip', ZipArchive::OVERWRITE|ZipArchive::CREATE); 

这为我解决了错误。