问题
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