0

当我尝试在本地系统中下载带有以下代码的 zip 文件时,该文件正在下载所有文件,但在服务器中它以 20 个字节下载,并且当提取文件损坏且未找到存档时。所以我添加了对此代码进行一些检查并运行,但这次找不到文件,进入此检查(如果(!is_file($archive_file_name)))。服务器上可能有什么问题?

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_path as $file_path)
        {
            if(basename($file_path) =="documents")
            {

                foreach($file_names as $value=>$files)
                {
                    $customer_files = $this->fetchCustomFieldValuesAndFieldNamesByOrder($files['order_id']);
                    if($customer_files['field_value'] !="" && file_exists($file_path.$customer_files['field_value']))
                    {
                        $file_extension = pathinfo($customer_files['field_value'], PATHINFO_EXTENSION);
                        $zip_file_name = $files['customer_name']."_"."Files.".$file_extension;
                        $zip->addFile($file_path.$customer_files['field_value'],$zip_file_name);
                    }
                    //echo $customer_files['field_value']."<br>";
                }
            } else
            {
                foreach($file_names as $value=>$files)
                {

                    if($files['file_name'] !="" && file_exists($file_path.$files['file_name']))
                {
                    $file_extension = pathinfo($files['file_name'], PATHINFO_EXTENSION);
                    $zip_file_name = $files['customer_name']."_"."Application".".$file_extension";
                    $zip->addFile($file_path.$files['file_name'],$zip_file_name);
                        //  echo $file_path.$files['file_name'],$zip_file_name."<br>";
                }

                }
        }
        }
        //  echo "numfiles: " . $zip->numFiles . "\n";
        //  echo "status:" . $zip->status . "\n";
        $zip->close();//exit;
     //then send the headers to foce download the zip file
        if (headers_sent()) {
            echo 'HTTP header already sent';
        } else {
            if (!is_file($archive_file_name)) {
                header($_SERVER['SERVER_PROTOCOL'].' 404 Not Found');
                echo 'File not found';
            } else if (!is_readable($archive_file_name)) {
                header($_SERVER['SERVER_PROTOCOL'].' 403 Forbidden');
                echo 'File not readable';
            } else {
                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;
            }
        }
            }
4

1 回答 1

2

如果您从 $zip->status 获得状态 11,则意味着您尝试压缩的文件不存在。以下是有关 Zip / ZipArchive 类的错误代码的文档:http: //php.net/manual/en/zip.constants.php

使用getcwd()验证您所在的路径是否正确,并且该路径上的文件确实存在。

于 2012-09-24T10:35:20.780 回答