Possible Duplicate:
forcing a file download with php
force download of different files
I am trying to allow users to download images from my site in php. I have seen a bunch of articles on it, and I was trying to use readfile from the php docs. But I can't seem to get it to work.
This is the code for the button that I am trying to set up to start the download:
<!-- download image button -->
<form class="grid_12" style="text-align: center;" action="../includes/download.php" method="post">
<input type="hidden" name="id" value="<?php echo $photo->id; ?>" />
<input type="hidden" name="img" value="<?php echo $photo->filename; ?>" />
<?php if(isset($_GET['cat'])){?>
<input type="hidden" name="cat" value="<?php echo $_GET['cat']; ?>" />
<?php } ?>
<div id="download"><input type="submit" name="submit" value="Download Photo" /></div>
</form>
And this is the code I have set up in my download.php file (redirect_to is just a function that calls header(Location: var) where the var is what you pass in.
<?php
require_once('initialize.php');
?>
<?php
header("Content-type: application/force-download; filename=".$_POST['img']);
// Force download of image file specified in URL query string and which
// is in the same directory as this script:
if(!empty($_POST['img']))
{
$file = $_POST['img'];
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');
header('Pragma: public');
header('Content-Length: ' . filesize($file));
ob_clean();
flush();
readfile($file);
exit;
}
$addy = "../public/photo.php?id=".$_POST['id'];
if(isset($_POST['cat'])){
$addy .= "&cat=".$_POST['cat'];
}
redirect_to($addy);
}
header("HTTP/1.0 404 Not Found");
?>
The hidden variables I am sending in the post are mostly for the redirection to take the user back to the page they downloaded from after the download starts. On my local host I get redirected to the page I clicked the download button on. On my server I just get sent to a blank page registering as download.php. I think this has some thing to do with output buffering potentially, because I am getting a blank page. Which generally means there was some kind of output before I call the header() function.
If anyone could give me some insight as to what I am doing wrong here I would greatly appreciate it, Thanks for reading.
-Alan