天天看點

php下載下傳二進制流檔案下載下傳,從MySQL流式傳輸二進制檔案以使用PHP下載下傳

我有一個存儲在MySQL表longblob字段中的Excel電子表格.我需要檢索此資料,然後将其作為可下載下傳檔案傳輸給使用者,最好不要先将其寫入磁盤.

可能?

編輯 – 呃,剛想通了……發表在下面的答案.

function getfile($blockid)

{

global $msa_db;

$sql = "select filename, filedata from blocks where blockid = '$blockid'";

$query = mysql_query($sql, $msa_db);

$result['filename'] = mysql_result($query,0,0);

$result['filedata'] = mysql_result($query,0,1);

return $result;

}

function download($fileinfo)

{

$file = base64_decode($fileinfo['filedata']);

header("Cache-Control: no-cache private");

header("Content-Description: File Transfer");

header('Content-disposition: attachment; filename='.$fileinfo['filename']);

header("Content-Type: application/vnd.ms-excel");

header("Content-Transfer-Encoding: binary");

header('Content-Length: '. strlen($file));

echo $file;

exit;

}

$fileinfo = getfile($blockid);

download($fileinfo);