Re: implementing downloads with php?
| From: | Tim Streater | Date: | Fri, 03 Apr 2015 09:18:00 +0000 |
| Subject: | Re: implementing downloads with php? | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-324763@lists.php.net to get a copy of this message | ||
On 02 Apr 2015 at 22:51, Jeffry Killen <jekillen@prodigy.net> wrote:
> This is new territory for me a maybe a bit off topic but
> I am interested in finding out how to implement a downloading
> mechanism with php and without using ftp (if possible).
I think you want something like this:
<?php
// This file could be called mydownloader.php
$file = $_POST['filename'];
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename=' . basename($file));
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 ($file));
ob_clean ();
flush ();
readfile ($file);
?>
In my case, after validating the user's choices, I have this form in a webpage:
<form action="mydownloader.php" method="post">
<input name="filename" value='/path/to/file' type=hidden>
<p style="padding-top: 2em; padding-bottom: 2em;"><input type="submit"
value="Download"></p>
</form>
You'll have to construct the value field, of course.
--
Cheers -- Tim