Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP: How to hide URL

Tags:

php

download

I have the following download.php script to download a file, which works great:

<?php

header("Content-Type: application/octet-stream");

$file = $_GET["file"];
header("Content-Disposition: attachment; filename=" . urlencode($file));   
header("Content-Type: application/force-download");
header("Content-Type: application/octet-stream");
header("Content-Type: application/download");
header("Content-Description: File Transfer");            
header("Content-Length: " . filesize($file));
flush(); 
$fp = fopen($file, "r");
while (!feof($fp))
{
    echo fread($fp, 65536);
    flush(); 
} 
fclose($fp);

?>

What I want to achieve is to hide the URL where this file is located, so that when the user clicks a link such as <a target="_blank" href="http://domain.com/files/download.php?file=filename.pdf">Download file</a>, a new tab opens up with no URL and starts downloding the file. What is actually happening is the new tab opens and the file download starts but the URL bar is displaying http://domain.com/files/download.php?file=filename.pdf.

If this cannot be done with php, how can I achieve this? I have seen several downloads where the URL is not shown, so I know this is somehow possible.

EDIT: Here is the reason I want to do this: We will send a html mailing with a link to the file download, and the website where this file download is hosted is not the website from the company which sends the mail.

As always, thank you very much.

like image 853
pablito.aven Avatar asked Jul 01 '26 00:07

pablito.aven


1 Answers

A typical way doing this, is to place the files you want to provide for download outside your docroot. Your download script should know about this place and has to process the requested filename considering this.

For example:

path/in/your/system/docroot/download.php and path/in/your/system/files/filename.pdf

If someone is requesting download.php?file=filename.pdf your script has to look up in path/in/your/system/files/ for this file and has to handle it.

like image 181
bukart Avatar answered Jul 02 '26 13:07

bukart



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!