Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create download link to download a file instead of redirecting to browser?

Tags:

html

php

I am trying to put a link to download a pdf from server, instead of redirecting the browser to open the pdf in another page. I have seen such options in many sites but unable to get the code. And most people says it should be possible with php only, could anyone help me for this.

like image 535
user1579340 Avatar asked Feb 19 '23 01:02

user1579340


1 Answers

Redirect it to a php page with this code:

$path_to_file = '/var/www/somefile.pdf'; //Path to file you want downloaded
$file_name = "somefile.pdf"; //Name of file for download
header('Pragma: public');   // required
header('Expires: 0');    // no cache
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header("Content-Type: application/octet-stream");
header('Content-Disposition: attachment; filename="'.$file_name.'"');
header('Content-Transfer-Encoding: binary');
readfile($path_to_file);
die();
like image 54
Pitchinnate Avatar answered Apr 26 '23 11:04

Pitchinnate