【问题标题】:php ziparchive readfile, no such file found errorphp ziparchive readfile, no such file found 错误
【发布时间】:2013-07-23 03:20:57
【问题描述】:

我遇到了这个错误:

[23-Jul-2013 16:26:15 America/New_York] 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); 
 ?>

【问题讨论】:

标签: file-io stream ziparchive php


【解决方案1】:

你有几件事错了 首先 ZIPARCHIVE::CREATE 不正确,它应该是 ZipArchive::CREATE 或 ZipArchive::OVERWRITE(如果您不删除 zip 文件,请覆盖)。

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

此外,我发现您的站点根文件夹不太可能是可写的。因此,我将 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); 
 ?>

【讨论】:

  • 更改文件夹的权限后仍然存在相同的错误。
  • 奇怪,我在发布之前运行了代码以确保它能够正常工作。您可以打印出路径并确保它们与您的文件夹结构正确匹配吗?还要确保正在创建 zip 文件。
  • 现在有一个针对此常见错误的故障排除清单:stackoverflow.com/a/36577021/2873507
【解决方案2】:

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

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

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

这解决了我的错误。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-11-01
    • 1970-01-01
    • 2020-03-19
    • 1970-01-01
    • 1970-01-01
    • 2023-01-22
    • 1970-01-01
    相关资源
    最近更新 更多