Domanda

I am encountering this error:

[23-Jul-2013 16:26:15 America/New_York] PHP Warning: readfile(Resumes.zip): failed to open stream: No such file or directory in /home/mcaplace/public_html/download.php on line 24

This is the code:

<?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); 
 ?>
È stato utile?

Soluzione

You have a couple things wrong First off ZIPARCHIVE::CREATE is not correct it should be ZipArchive::CREATE or ZipArchive::OVERWRITE (overwrite if your not deleting the zip file).

Next your pathing is wrong. You are not telling readfile() the absolute path to the file. So you need to give absolute paths to the where the zip file is saved to and where it's read from.

Also I find it unlikel that your root folder for the site is writable. So i moved the zip into the resumes folder for the code below. I tend to stay away from $_SERVER['DOCUMENT_ROOT'] my self and use the realpath() function.

And lastly you have to make sure what ever folder the zip file is being created into that it has the 777 permissions on it.

Here is the complete code change

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

Altri suggerimenti

I had a similar problem with PHP 7.0 and could fix it using the solution from OHCC here: http://php.net/manual/de/ziparchive.open.php#118273

He was suggesting to use both flags OVERWRITE and CREATE at the same time.

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

This solved the error for me.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top