2009-10-13 131 views
4

我有Excel電子表格存儲在MySQL表longblob字段。我需要檢索這些數據,然後將其作爲可下載文件流式傳輸給用戶,最好不要先寫入磁盤。從MySQL下載流二進制文件與PHP下載

可能嗎?

編輯 - 呃,只是想通了......發表在下面的答案。

回答

11
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); 
+1

感謝分享。這可能有用一天:o) –

+0

我正在尋找這方面的東西。謝啦。 –

1

就得到了解決

http://www.codeproject.com/Articles/831185/CREATE-DOWNLOAD-ZIP-FILE-USING-PHP

$data = base64_decode($data); 

    header("Cache-Control: no-cache private"); 
    header("Content-Description: File Transfer"); 
    header("Content-disposition: attachment; filename='".$identifier.".zip'");  
    header("Content-Type: application/octet-stream"); 
    header("Content-Transfer-Encoding: binary"); 
    header('Content-Length: '. strlen($data));  
    header("Pragma: no-cache"); 
    header("Expires: 0"); 

    ob_clean(); 
    flush(); 

    echo $data; 
+0

感謝您的解決方案。它與7年前公認的解決方案相比有何改進? – Ian