Zip文件不能在PHP中下载

时间:2022-10-12 09:56:35

I've implemented the code to Create Zip Folder of Files (from db path) and download zipped folder in PHP. I am using Ubuntu OS.

我已经实现了创建文件Zip文件夹的代码(来自db路径)并在PHP中下载了压缩文件夹。我正在使用Ubuntu OS。

public function actionDownload($id) {
            $model = $this->loadModel($id, 'Document');
            $results = array();
            $results = $this->createZip($model);

            $zip = $results[0];
            $size = filesize($zip->filename);

            if ($zip->filename) {             
                header("Content-Description: File Transfer");
                header("Content-type: application/zip");
                header("Content-Disposition: attachment; filename=\"" . $model->name . "\"");
                header("Content-Transfer-Encoding: binary");
                header("Content-Length: " . $size);

                ob_end_flush();
                flush();
                readfile($zip->filename);
                // To Store User Transaction Data
                //$this->saveTransaction();
                //ignore_user_abort(true);
                unlink($zip->filename);
                $zip->close();

                // Delete newly created files
                foreach ($results[1] as $fl) {
                    unlink($fl);
                }
            }       
    }


public function createZip($model) {

        $data = Document::model()->findAll('parent_folder=:id', array(':id' => (int) $model->document_id));
        $fileArr = array();
        foreach ($data as $type) {
            $fileArr[] = $type->path;
        }

        $filestozip = $fileArr; // FILES ARRAY TO ZIP
        $path = Yii::app()->basePath . DS . 'uploads' . DS . Yii::app()->user->id;

        //$model->path = trim(DS . $path . DS); // DIR NAME TO MOVE THE ZIPPED FILES        

        $zip = new ZipArchive();
        $files = $filestozip;
        $zipName = "USR_" . Yii::app()->user->id . "_" . $model->name . "_" . date("Y-m-d") . ".zip";

        $fizip = $path . DS . $zipName;
        if ($zip->open($fizip, ZipArchive::CREATE) === TRUE) {
            foreach ($files as $fl) {
                if (file_exists($fl)) {
                    $zip->addFile($fl, basename($fl)) or die("<p class='warning'>ERROR: Could not add file: " . $fl . "</p>");
                }
            }
        }

        $resultArr = array();
        $resultArr[] = $zip;
        $resultArr[] = $files;

        return $resultArr;
    }

The Zip creation code working fine and its creating zip file there but the issue is owner of the file is www-data and file permission is Read-Only.

Zip创建代码工作正常,并在那里创建zip文件,但问题是文件的所有者是www-data,文件权限是只读的。

When I am trying to set chmod($zip->filename, 0777) permission to that zipped folder then its showing an error?

当我尝试将chmod($ zip-> filename,0777)权限设置为该压缩文件夹时,它显示错误?

Error 500
chmod(): No such file or directory

In fact file is present there.

实际上文件存在于那里。

If I am trying without chmod() then its showing me error of

如果我在没有chmod()的情况下尝试,那么它会显示我的错误

Error 500
filesize(): stat failed for /home/demo.user/myapp/public_html/backend/uploads/1/USR_1_kj_2013-12-23.zip

and then its not downloading the zip file.

然后它不下载zip文件。

This is really weird issue I am facing. It seem to be some permission issue of zip file that's why filesize() is not able to perform any operation on that file but strange thing is chmod() also not working.

这是我面临的非常奇怪的问题。这似乎是zip文件的一些权限问题,这就是为什么filesize()无法对该文件执行任何操作,但奇怪的是chmod()也无法正常工作。

Need Help on this.

需要帮助。

Thanks

1 个解决方案

#1


0  

If www-data has read permissions, the file permissions are properly set. No chmod required.

如果www-data具有读取权限,则会正确设置文件权限。不需要chmod。

You need to call $zip->close() before the download, as only on $zip->close() the file will be written to disk. readfile() will not work unless the file is written to disk.

你需要在下载之前调用$ zip-> close(),因为只有$ zip-> close()文件才会被写入磁盘。除非将文件写入磁盘,否则readfile()将无法工作。


A smarter way, could be to create the archive in memory only using the php://memory stream wrapper. However this is only an option if you need the archive for a single download only.

更聪明的方法是,只能使用php://内存流包装器在内存中创建存档。但是,如果您只需要单个下载的存档,这只是一个选项。

#1


0  

If www-data has read permissions, the file permissions are properly set. No chmod required.

如果www-data具有读取权限,则会正确设置文件权限。不需要chmod。

You need to call $zip->close() before the download, as only on $zip->close() the file will be written to disk. readfile() will not work unless the file is written to disk.

你需要在下载之前调用$ zip-> close(),因为只有$ zip-> close()文件才会被写入磁盘。除非将文件写入磁盘,否则readfile()将无法工作。


A smarter way, could be to create the archive in memory only using the php://memory stream wrapper. However this is only an option if you need the archive for a single download only.

更聪明的方法是,只能使用php://内存流包装器在内存中创建存档。但是,如果您只需要单个下载的存档,这只是一个选项。