skinme!
07-30-2002, 01:53 PM
How can I use PHP to send a file to the user (i.e. Pop-up a download box)?
I have a page (download.php) that has a string passed to it called id. It will use mySQL to find out the URL of the file to be downloaded. I can do all this. I just don't know how to then make the file available to the user to download.
Sounds simple, huh?
Spookster
07-30-2002, 02:11 PM
Well what you can do is do like most software download sites do and take the file and generate a page like so:
<html>
<head>
<meta http-equiv="refresh" content="3, <?php echo $filename ?>">
</head>
<body>
<h3>Your file will begin downloading in a moment.....</h3>
</body>
</html>
Putting the file name into the location part of a meta refresh tag will cause the download prompt box to open up.
skinme!
07-30-2002, 02:43 PM
Is there not a way of doing this without changing the current page (ie by not opening a new page)?
For example, on DeviantArt (a site I use daily) you click on a download link and although it links to a new page (download.php?id=xxx rather than browse.php?section=blah) the current page doesn't change:
http://www.deviantart.com/download.php?id=557223 and click on the big picture of the winamp2 skin.
Thanks.
skinme!
07-30-2002, 11:00 PM
Hmm... I'm having problems with the method you gave up there, Spookster.
I've tested it to check it outputs the correct URL by moving it the body and echoing the string containing the URL.
However, when I run the script as you suggested, outputing the URL as the second argument in the HTTP request, I do not get a download box. I see the blue progress bar in the status bar go to full every 3seconds. :confused:
Spookster
07-31-2002, 12:48 AM
Slight typo on my part with the meta tag.
<meta http-equiv="refresh" content="3, URL= <?php echo $filename ?>">
skinme!
07-31-2002, 12:50 AM
Thanks. I did think to check the META tag, but I got distracted.
skinme!
07-31-2002, 09:03 PM
I just thought I'd share the solution I arrived at:
download.php
<?php
header('Cache-Control: no-cache, must-revalidate');
header('Pragma: no-cache');
header('Content-type: application/octet-stream');
header("Content-Disposition: inline; filename=\"".$filename."\"");
header('Content-length: '.(string)(filesize($path2file)));
$fd=@fopen($path2file,'r');
fpassthru($fd);
?>
Where $filename contains "myfile.zip" and $path2file = $DOCUMENT_ROOT."/files/".$filename (or whatever the path to the file is).
Please note: there is nothing but this code in the file (ie no HTML). It will turn itself into myfile.zip and use HTTP to identify itself as a file to be downloaded.
azzaka shadowbr
10-16-2004, 03:19 AM
Ok this wasn't written by me, however I found it extremly usefull.
Original Author: Author: Robert Fogt
E-mail: webmaster@bluesparks.com
<?
// The URL to the directory where all the files are located.
// Do not add a / at the end of the url.
$filedirectory = 'http://downloads.yourdomain.com';
// The Path to the directory where all files are located.
// Do not add a / at the end of the path.
$filepath = '/home/username/public_html/downloads';
// The template filename. Place in same directory as the download.php file.
$template = 'index.html';
///////////////////////////////////
//////////// End setup ////////////
///////////////////////////////////
// For our error messages
function die_nice($message){
die("
<html><head><title>ERROR!!</title></head><body>
<p> </p>
<center>
<table border=\"1\" width=\"400\" bordercolor=\"#FF0000\" cellspacing=\"0\" cellpadding=\"3\">
<tr>
<td><font color=\"#000080\" face=\"Arial\">The program encountered an Error!!</font></td>
</tr>
<tr>
<td><p> </p>
<p><font face=\"Arial\"><b>$message</b></font></p>
<p> </p></td>
</tr>
</table>
</center>
</body></html>
");
}
// Check to see if the program was called correctly
if (!$file) die_nice("Program was called incorrectly, it should be called like:<br>download.php?file=filename");
// Check to see if the file the want to download exists
if ( !file_exists("$filepath/$file") ) die_nice("The file you are trying to download does not exist:<br>$file");
// read in the template file
$fp = fopen ("$template", "r") or die_nice("Could not open template file: $template");
$contents = fread ($fp, filesize ($template));
fclose ($fp);
// replace the macros
$contents = str_replace("[FILENAME]", $file, $contents);
$contents = str_replace("[FILESIZE]", number_format(filesize("$filepath/$file")), $contents);
$contents = str_replace("[MODIFIEDDATE]", date("F j Y", filemtime("$filepath/$file")), $contents);
$contents = str_replace("[DOWNLOAD]", "${filedirectory}/$file", $contents);
// Now display the template
print stripslashes($contents);
?>
Ok once you have your .php code written the all you need is the .html template to be used.
Set the variables in the .html template and b0bs your uncle.
The html code:
FileName: [FILENAME]
FileSize: [FILESIZE] bytes
Last Modified: [MODIFIEDDATE]
<a href="[DOWNLOAD]">I agree - Download the File</a>
The best thing about the html part of this code is that you can use any formating you choose as long as you include the variables.
Example: Download HTML Template (http://downloads.telusdesigns.com/)
Hope this is useful. :)
Azzaka