Qbasicnews.com

Full Version: File downloads (PHP)
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
In php, how do you trigger a download? For instance, with QBNZ's downloads, when you click one, it links to a php page.
Simple. Create a new PHP file and put this in it...

Code:
<?
    header("Location: http://www.qbasicnews.com");
?>

Now upload it and run it. See? Now, change that PHP script and put this instead...

Code:
<?
    header("Location: http://www.somesite.com/somezipfile.zip");
?>

(Of course make the URL a valid one Wink). Simple, no?

EDIT: Important: Don't forget... if you are going to use the header() function then you must remember to not output anything to the browser BEFORE the call to header(). If you do then you will get errors and it won't work at all.
On a side note, the code to the QBNZ download page (roughly):

Code:
<?php
$download_dir = "backup/media/";

$dbhost = '[censored]';
$dbname = '[censored]';
$dbuser = '[censored]';
$dbpasswd = '[censored]';
$dbc = mysql_connect($dbhost, $dbuser, $dbpasswd) or die('Error: ' . __LINE__ . ' ' . mysql_error());
mysql_select_db($dbname) or die('Error: '. __LINE__ . ' ' . mysql_error());

$sql = "SELECT file_name, extern FROM " . DOWNLOADS_TABLE . " WHERE id = '$id'";
$result = mysql_query($sql) or die('Error: ' . __LINE__ . mysql_error());

$row = mysql_fetch_array($result) or die('Error: ' . __LINE__ . mysql_error());

$file_name = $row['file_name'];
$external = $row['extern'];
if (!$external == '1')
{
  $file_size = filesize($download_dir . $file_name);
}
else
{
  $file_size = 'Unknown';
}


$sql = "UPDATE forum_downloads SET clicks=clicks+1 WHERE id = '$id'";
$result = mysql_query($sql);

if ( $external == '1' )
{
  header("Location: $file_name");
}

header("Content-Type: application/octet-stream");
header("Content-Disposition: attachment; filename=\"$file_name\"");
if (!$file_extern == '1')
{
  readfile($download_dir . "/" . $file_name);
}
else
{
  readfile($file_name);
}

?>

On another side note, you may have to check when using the location header - HTTP/1.0 can't handle it.
I just insert a meta redirect into the HTML via a PHP print, and provide a link on the main page which points to the file in case the browser doesn't redirect to the file. This method works for any HTTP version, and is the technique I used for TBN. Smile
Fling: OK, and then after that I can output stuff saying "You are downloading file blah blah"?
And Oracle: I could never figure out mySQL. :normal:
Ado: Yes, yes. Although the same question as with Fling stands.
Possibly you can. I'm not too sure how though - best way is to make the links to downloads open in a new window.

And ado - HTML redirects are messy, some browsers can't handle them. Better to use server-side where possible (unless you have to use HTTP/1.0)
I know some ancient should-have-been-buried-a-long-time-ago browsers can't handle html redirects, hence the direct link on the webpage Wink I have another reason for using the redirect though, so I use it that way for that reason. Smile Otherwise, I'd use the server-side redirect.
It's not just ancient browsers... you can set whether you want to use 1.1 or 1.0 in most browsers anyway.

Anyway, I think the serverside redirect is nicer (faster, anyway), but I'm sure you have good reasons Wink