Force downloading file in php

家住魔仙堡 提交于 2021-02-10 14:44:57

问题


I have just finished my project and move to live server mean-while all contents and program are working very well on my Development section (XAMPP) and I have also moved this project to test on a live shared hosting server (Namecheap). all work good then until I purchase a VPS server and installed cpanel. When I uploaded my project and using php 7.3, things no longer are working right for file forcing download. Here is my code.

//* Download item zip files
public function downloadItemZipFiles($id)
{
    $settingsModel = new AdminPanel();
    $itemModel = new ItemsModel();
    $siteInfo = $settingsModel->getApplicationInfomations();
    $item = $itemModel->getItemByIdOnly($id);
    $name = $siteInfo->ss_name . '-' . $item->item_id . '-' . $item->item_slug.'.zip';
    $path = item_zip_file($item->item_id);

    if (is_file($path)) {
        if (ini_get('zlib.output_compression')) {
            ini_set('zlib.output_compression', 'Off');
        }

        $file = new \CodeIgniter\Files\File(strval($path));
        $mime = $file->guessExtension();
        header('Pragma: public');
        header('Expires: 0');
        header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
        header('Last-Modified: '.gmdate('D, d M Y H:i:s', filemtime($path)).' GMT');
        header('Cache-Control: private', false);
        header('Content-Type: '.$mime);
        header('Content-Disposition: attachment; filename="'.basename($name).'"');
        header('Content-Transfer-Encoding: binary');
        // header('Content-Length: '.filesize($path));
        header('Connection: close');
        readfile($path);
        exit();
    }
    else
    {
        echo "not found";
    }
}

if I try to force download a file, I receive this browser error

This site can’t be reachedThe webpage at https://www.codewigs.com/filex/download-item-zip-file/21 might be temporarily down or it may have moved permanently to a new web address. ERR_INVALID_RESPONSE

How can I overcome this error?

来源:https://stackoverflow.com/questions/65372637/force-downloading-file-in-php

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!