I am making a PHP script that will download a file given a name and a version. The files will be stored on the server like this:
/dl/Project1/1.0.txt
/dl/Project1/1.1.txt
/dl/Project2/2.3.jar
/dl/Project2/2.3.1.jar
And the paths to retrieve such files would look like this:
download.php?name=Project1&type=txt&version=1.0
download.php?name=Project1&type=txt&version=1.1
download.php?name=Project2&type=jar&version=2.3
download.php?name=Project2&type=jar&version=2.3.1
The issue arises when actually downloading the files. In this example, I want the first two files to both download as Project1.txt, and I want the last two to download as Project2.jar. How can I rename these temporarily to allow this to work?
Send out a header defining the file's name.
$filename = $name . "." . $type;
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename=' . $filename);
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($file));
I've included additional headers, since you should send out these as well. This is just a modified example of what you can see in the PHP documentation on readfile
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With