Answer the question
In order to leave comments, you need to log in
How to make the code on the server work after entering the header ()?
I have a problem such that on my local code the code passes normally header("Content-Type: $FileMime");
And after it the file is deleted as it should, but on the server it happens that when it comes to this header, the code simply stops processing.
My code
/**
* @param string $path
* @param string $filename
* @param string $contentDisposition
* @return bool
* @throws Exception
* @throws \Exception
*/
public static function DownloadDocument($path, $filename = 'documents', $contentDisposition = 'inline')
{
if (!file_exists($path)) {
throw new \Exception('File ' . $path . ' not found');
}
$FileMime = mime_content_type($path);
// сбрасываем буфер вывода PHP, чтобы избежать переполнения памяти выделенной под скрипт
if (ob_get_level()) {
ob_end_clean();
}
header("Content-Description: File Transfer");
header("Content-Type: $FileMime");
header("Content-Transfer-Encoding: binary");
header("Content-Length: ".filesize($path));
header("Content-Disposition: $contentDisposition; filename=$filename." . self::GetExtension($path));
header("Expires: 0");
header("Cache-Control: must-revalidate");
header("Pragma: public");
readfile($path);
return true;
}
/**
* @param int $id
* @return bool
* @throws Exception
*/
public static function DocumentStatisticsUtm($id)
{
$fileName = $id . '.xlsx';
$path = Yii::getAlias('@uploads') . '/temp/' . $fileName;
if (file_exists($path)) {
self::DownloadDocument($path);
unlink($path);
return true;
}
return false;
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question