1

このエラーが発生しています:

[2013 年 7 月 23 日 16:26:15 America/New_York] PHP 警告: readfile(Resumes.zip): ストリームを開くことができませんでした: No such file or directory in /home/mcaplace/public_html/download.php 行 24

これはコードです:

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

2 に答える 2

2

いくつか問題があります。最初に 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); 
 ?>
于 2013-07-23T21:00:26.200 に答える
0

私は PHP 7.0 で同様の問題を抱えていましたが、OHCC のソリューションを使用して修正できました: http://php.net/manual/de/ziparchive.open.php#118273

彼は、フラグ OVERWRITE と CREATE の両方を同時に使用することを提案していました。

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

これでエラーが解決しました。

于 2018-05-07T11:11:57.413 に答える