0

I have a problem with php force download. I tried a lot of code but no one resolve problem. I need to download file with php (the name of file is sent with querystring and files are stored in a folder protected by htaccess). With this code I can download for example a pics (the browser correctly ask with file dialog where saving it) but when I try to download a pdf file the browser don't open file dialog and save it in default download folder and automatically open pdf with default pdf viewer. Is there a possibility to open file dialog for pdf file and download like other file like pics without automatic download and opening it?

$file = $_GET['file'];
$url = "{$_SERVER['DOCUMENT_ROOT']}images/files/".$file;
header("Pragma: public");
header("Expires: 0");
header("Pragma: no-cache"); 
header("Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0");  
header("Content-Type: application/force-download"); 
header('Content-disposition: attachment; filename=' . basename($url));
header("Content-Transfer-Encoding: binary");
header('Content-Length: ' . filesize($url));
@readfile($url);
  exit(0);
  • https://stackoverflow.com/a/6794321/4248328 – Alive to die - Anant May 25 '22 at 12:25
  • Why two `Content-Type`s? Should use octet-stream to force the download; `application/pdf` will have the browser choose its behavior for that type. – user3783243 May 25 '22 at 12:26
  • Why not use the octet-stream type? https://stackoverflow.com/questions/10615797/utility-of-http-header-content-type-application-force-download-for-mobile or https://stackoverflow.com/questions/7263923/how-to-force-file-download-with-php – user3783243 May 25 '22 at 13:14
  • I tried also applicaion/pdf raher I used header('Content-Type: '.mime_content_type($url)); but the problem persists – Tarvobarros May 25 '22 at 14:16
  • `applicaion/pdf` is a typo, and if typed correct would tell the browser to use whatever it uses for PDFs. In Chrome for example they have a PDF reader – user3783243 May 25 '22 at 14:41
  • I don't want that browser don't ask where save pdf (while for pics, for example, it ask) and then open it. I need he browser ask where saving pdf and don't open directly. – Tarvobarros May 25 '22 at 15:10

0 Answers0