0

When I do a Google search I find a TON of replies about the code needed to download a file when clicked and that works just fine. What I am looking for is when I click on the link it will just open up the file in the browser instead of prompting to download the file. Here is the working code to Download a file:

$file = $invoice_dir . '/test.xlsx';

header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename="'.basename($file).'"');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($file));
readfile($file);
exit;

How do I just make it open up instead using my local software? It is an XLSX Office type file not plain text. I tried include() and it outputs jibberish.

user3187352
  • 39
  • 1
  • 1
  • 6
  • 2
    An `XLSX` can't play in a browser. You'll need to parse it and serve it in a format a browser can play. – user3783243 Aug 30 '19 at 05:41
  • I dont want to display it in the browser. I want it to open up my Office program on my computer without the prompt to download it first. If I click directly on the file it does that, but for security reasons I dont want people to know where that file is located so I am using a php file as a redirect. Years ago I was able to do this with an MP3 file where you clicked on it and it began streaming on the person's music player on their computer. If I remember correctly the header had to be different in order to cause that – user3187352 Aug 30 '19 at 05:49
  • I changed: "application/octet-stream" to: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" and it didn't change unless there was something else I needed to do – user3187352 Aug 30 '19 at 21:38

2 Answers2

1

You just needed to open the file in a href like this

<a href"http://www.yourdomain.com/index.php?file=file.pdf">View File</a>

and in case you want to open it in a new browser tab then

<a href"http://www.yourdomain.com/index.php?file=file.pdf" target="_blank">show file</a>
BlackXero
  • 880
  • 4
  • 17
0

Add this code

header("Content-type: application/vnd-ms-excel");