'How can I create a Download Page with post php method?
I want to know that how can I create a download page in PHP that download files without reveling the download link to the file, and source to the file and download page is put in the web page in the
<form>...</form>
tags..
Like:
<form action="example.com/download.php" method="post" target="_blank">
<input name="id" type="hidden" value="#">
<input name="filename" type="hidden" value="#">
<input name="filesize" type="hidden" value="#"><p></p><div
align="center"> > > <input type="image"> </div> </form>
The last input is the src of the download button image...
Can any body help me that how this whole process works, and what to put in the source code of the /download.php page.... And how to make forms for my webpages...
Actually I have seen all this on a website, who is doing the same thing..
Please help. Thanks in advance...
Solution 1:[1]
You want to create a generic download helper, where you can give some kind of a unique id and have this helper return the correct file to the end-user without revealing the real path to the file.
There are many ways to tackle this, it is also depends on what you are trying to achieve, and how many files will be available to download (this will affect the simplicity or complexity of the implementation)
For an example:
if you have a download site which will serves thousands of files - you will want to implement a method that will work well for N amount of files, this might get complex (depends on your requirements of course).
On the other hand - if you only intend to server 5-10 files, you might create a very simple helper that will get the job done.
A short example of a simple helper:
download.php
<?php
$fileId = $_GET['fileId'];
switch($fileId){
case 1:
$fileName = 'myFile.pdf';
break;
case 2:
$fileName = 'myOtherFile.doc';
break;
default:
$fileName = null;
}
if($fileName){
// $fileName is your file identifier
$fileFullPath = $_SERVER['DOCUMENT_ROOT'].'mySecretFolder/files/'.$fileName;
if (file_exists($fileFullPath)) {
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename='.basename($fileFullPath));
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
header('Content-Length: ' . filesize($fileFullPath));
ob_clean();
flush();
readfile($fileFullPath);
exit;
}
}
?>
HTML
<a href="https://www.my-domain.com/download.php?fileId=1"> Download PDF </a>
This is only a simple example to use if you need to serve a small amount of files and wish to make a simple, quick implementation.
In case you are going to server an unlimited or unknown amount of files, you will need to implement an advanced file serving system (or use an existing one that is open-source).
Hope it helps a bit
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 |