How can I download a file stored on the server
I have a form in PHP that allows a user to send emails with attachments in it. They are stored in a file which is uploaded on the server.
I can see the list of all the files that are sent by a user X, but I want to download the file and display it in a window to save when he clicks on the file if he wants.
Re: How can I download a file stored on the server
Use output() method of class FPDF. It allows either to send the file directly in the browser or save it locally on the disk in PDF format. You can then save it to disk and create a download link on your page instead of displaying the file.
Re: How can I download a file stored on the server
This code should work for you. It basically forces the file download.
Code:
$file = $data ['name'];
$path = './'. $file;
if (file_exists ($path))
{
header ('Content-disposition: attachment; filename="'. $file.'"');
header ('Content-Type: application/force-download');
header ('Content-Transfer-Encoding:binary');
header ('Content-Length:'.filesize ($path));
header ('Pragma: no-cache');
header ('Cache-Control: must-revalidate, post-check = 0, pre-check = 0');
header ('Expires:0');
readfile ($path);
}
else
{
$errorFile = 'The file'. $file. "n\ 'exist. Please excuse us for the inconvenience. ';
}