Downloading empty file
For the life of me I can't see why, but it has the correct file name and all, but the files themselves are empty after download. As far as I can see its going to the correct spot, the database has the correct info too.
function download()
{
global $session,$project,$config,$args;
$fid = $args[0];
$query = query('SELECT * FROM files WHERE id = ?',$fid);
if(num($query) == 1)
{
$file = fetch($query);
//$happyfix = "http://imengine.gofreeserve.com/admin";
$filename = URL . "/_files/{$file->in_project}/{$file->in_folder}/" . md5($fid) . ".{$file->extension}";
//$filename = $happyfix . "/_files/{$file->in_project}/{$file->in_folder}/" . md5($fid) . ".{$file->extension}";
if(ini_get('zlib.output_compression'))
{
ini_set('zlib.output_compression', 'Off');
}
header("Pragma: public"); // required
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: private",false); // required for certain browsers
header("Content-Type: $file->mimetype");
// change, added quotes to allow spaces in filenames, by Rajkumar Singh
header("Content-Disposition: attachment; filename=\"".$file->name . '.' . $file->extension."\";" );
header("Content-Transfer-Encoding: binary");
header("Content-Length开发者_StackOverflow中文版: ".filesize($filename));
readfile("$filename");
exit();
}
else
{
redirect('That file does not exist','','error');
}
}
You are not sending the headers before sending the file data. Change your last couple lines to add
header("Content-Length: ".filesize($filename));
ob_clean();
flush();
readfile("$filename");
See the example at http://php.net/manual/en/function.readfile.php
Change this:
header("Content-Type: $file->mimetype");
to:
header("Content-Type: {$file->mimetype}");
精彩评论