简体   繁体   中英

Zip file not downloading in PHP

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.

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 .

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

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

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.

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.

Need Help on this.

Thanks

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

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.


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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM