Forum Moderators: coopster
Specify the filename in $filename then execute send_download($filename) where:
function send_download($filename){
$file_path = '../myfiles/' . $filename;
$file_size=@filesize($file_path);
header("Content-Type: application/x-zip-compressed");
header("Content-disposition: attachment; filename=$filename");
header("Content-Length: $file_size");
readfile($file_path);
exit;
}
This will need to be done before your script sends any other information to the browser.
The only catch is the function above assumes the file ($filename) is a .zip file (i.e., $filename = something.zip). If this is true in your case then you should not have to modify anything else. If you are sending some other type of file, you will need to change "application/x-zip-compressed" to reflect a different Mime type. For example:
'application/x-zip-compressed' is mime type for a .zip file
'application/x-msdownload' is mime type for a .exe file
So you have a page called 'get_file.php' (for example). On this page you stick your function as above, maybe modified so that you can use GET to send the $filename. Then once the file has been got and sent to the browser use header('Location: ...'); to send your visitors to another page.
Then download.php would contain:
if (isset($_GET['file'])){
$file = $_GET['file']; # $file should be 10
$query = "SELECT FileName FROM Mytable WHERE FileNumber='$file'";
$result = @mysql_query($query);
$row = mysql_fetch_array($result);
$filename = $row[FileName];
send_download($filename);
} else {
echo "Error";
)
# Declare function
function send_download($filename){
$file_path = '../myfiles/' . $filename;
$file_size=@filesize($file_path);
header("Content-Type: application/x-zip-compressed");
header("Content-disposition: attachment; filename=$filename");
header("Content-Length: $file_size");
readfile($file_path);
exit;
}
The only thing I would add is that as the browser will be directed to download.php this page need to do somethings. As at the moment you will either get 'Error' or a completely blank screen.
So just before the exit add header('Location: another_page.php'); So that your users are sent to another page.
Are you talking about a row in a HTML table or a row in your database? Not sure what you are asking.
If you look, files.php is the page with the script, and when you click it, the box will appear with the download. It will say its a .jpg image, but that is not the correct image for the row. I'm pretty sure thats the first file in the table (on the database not html table). It's not taking out the 10th file, as the url is: ?id=10. I want it to get the file for that row in the table from the database, that the page is getting from.
Ok now I get this error:
Warning: Cannot modify header information fo rline 22, 23 , and 24. :/
[edited by: eelixduppy at 1:02 am (utc) on Feb. 25, 2008]
[edit reason] no URLs, please [/edit]
I would put a field in the database which is ID and then change the query to:
$query = "SELECT FileName FROM Mytable WHERE ID='$file'";
Then, it will find the FileName corresponding to the row with the proper ID, regardless of which row it is (your rows can be mixed up and it will still work properly - don't count on your rows remaining in some particular order with SQL).
Make sure that your ID in the database corresponds to the row containing the desired file for <a href="download.php?file=10">asd</a>
The header warning probably means you are attempting to send headers to the browser more than once. Rather than explain this it would probably be easier if I gave you a simple rule: You can only send headers once per webpage and it must be done before any other text is sent to the browser. My guess is that you are echoing or printing some HTML, text, or possibly an inadvertent space character prior to the header() lines.