File io php ziparchive readfile,未找到此类文件错误

File io php ziparchive readfile,未找到此类文件错误,file-io,stream,ziparchive,php,File Io,Stream,Ziparchive,Php,我遇到了这个错误: [23-Jul-2013 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_n

我遇到了这个错误:

[23-Jul-2013 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文件的保存位置和读取位置提供绝对路径

此外,我发现您的站点根文件夹不可能是可写的。因此,我将压缩文件移到了简历文件夹中,以获取下面的代码。我倾向于远离$\u SERVER['DOCUMENT\u 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的解决方案来解决:

他建议同时使用覆盖和创建两个标志

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

这为我解决了错误。

即使更改了文件夹的权限,同样的错误仍然存在。奇怪的是,我在发布之前运行了代码以确保它正常工作。您能打印出路径并确保它们与您的文件夹结构正确匹配吗?还要确保正在创建zip文件。现在这里有一个针对此常见错误的故障排除清单:stackoverflow.com/a/36577021/2873507One经常遇到此错误,要快速排除此错误,请执行以下步骤: