tags:

views:

1656

answers:

3

I'm making a file sharing site for the fun. Trying to make it so that when I hit download, it starts the download. Instead of just a link to /files/$file im trying to do a header redirect:

download.php

/**
* File Download
*/

$query = mysql_query("SELECT id,name,desc FROM files WHERE id = ".intval($_GET['id']));
$row = mysql_fetch_assoc($query);

$file = $row['name'];

if (file_exists($file)) {
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);
exit;
}

?>

Filename: <?=$row['name']?>
Desc: <?=$row['desc']?>

<a href="#">Download this file</a>

Im stuck here, what should I do next?

thank you

A: 

You can only do a redirect before any html has been pushed.

Basically after

Filename: <?=$row['name']?>
Desc: <?=$row['desc']?>

<a href="#">Download this file</a>

is sent to the browser, your chance to perform a redirect with php is over. Maybe a Javascript redirect is more appropriate? Tutorial on Javascript redirects.

asperous.us
+1  A: 

I'm not following what you are asking. A header redirect looks like:

header("Location: /path/to/new/page");

If you want the existing info to download, looks like you need to hit it with an id in your url:

<a href="file.php?id=<?=$file_id ?>">

where $file_id has the id of the file you are looking for. Personally, I'd put that in another file from the page that shows the listing and download link, but YMMV.

DGM
Per RFC 2616 http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.30, you should use an absolute URL.
michaelc
+1  A: 

Do you mean something like this, where the page will display the information about the file and then when the user has clicked th link it will download it?

<?php

$query = mysql_query("SELECT id,name,desc FROM files WHERE id = ".intval($_GET['id']));
$row = mysql_fetch_assoc($query);

$file = $row['name'];

if(!file_exists($file))
{
    exit('File does not exist');
}

if(intval($_GET['download'])===1)
{
    // Generate the server headers
    if (strstr($_SERVER['HTTP_USER_AGENT'], "MSIE"))
    {
        header('Content-Type: "application/octet-stream"');
        header('Content-Disposition: attachment; filename="'.basename($file).'"');
        header('Expires: 0');
        header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
        header("Content-Transfer-Encoding: binary");
        header('Pragma: public');
        header("Content-Length: ".filesize($file));
    }
    else
    {
        header('Content-Type: "application/octet-stream"');
        header('Content-Disposition: attachment; filename="'.basename($file).'"');
        header("Content-Transfer-Encoding: binary");
        header('Expires: 0');
        header('Pragma: no-cache');
        header("Content-Length: ".filesize($file));
    }
    $data = readfile($file);
    exit($data);
}
else
{
?>
    Filename: <?php echo $row['name']; ?>
    Desc: <?php echo $row['desc']; ?>
    <a href="<?php echo $_SERVER['SCRIPT_NAME']; ?>?id=<?php echo $row['id']; ?>&download=1">Download this file</a>
    <?php
}
?>
Paul Dixon