я хочу скачивать файлы, когда нажимаю на имя изображения или имя файла в php - PullRequest
0 голосов
/ 17 декабря 2018
$path = "https://surv-translation.com/assets/uploads/files/";
$filename = "1544615073screenshot-trustwortha.com-2018.11.29-16-37-46.png";
header('Content-Transfer-Encoding: binary');  // For Gecko browsers mainly
header('Last-Modified: ' . gmdate('D, d M Y H:i:s', filemtime($path)) . ' GMT');
header('Accept-Ranges: bytes');  // For download resume
header('Content-Length: ' . filesize($path));  // File size
header('Content-Encoding: none');
header('Content-Type: application/pdf');  // Change this mime type if the file is not PDF
header('Content-Disposition: attachment; filename=' . $filename);  // Make the browser display the Save As dialog
readfile($path);
exit;

Я пытаюсь использовать этот код, но у меня ничего не получилось, и я хочу загрузить любой файл, такой как docx, pdf, ppt или изображение и т. Д., Поэтому не могли бы вы мне помочь, пожалуйста?

1 Ответ

0 голосов
/ 17 декабря 2018

Вы можете попробовать этот код для загрузки любого файла, например doc , pdf , ppt или image и т. Д.

if (file_exists($filepath)) {
 header('Content-Description: File Transfer');
 header('Content-Type: application/octet-stream');
 header('Content-Disposition: attachment; filename="' . basename($filepath) . '"');
 header('Expires: 0');
 header('Cache-Control: must-revalidate');
 header('Pragma: public');
 header('Content-Length: ' . filesize($filepath));
 flush(); // Flush system output buffer
 readfile($filepath);
 exit;
 }
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...