2009-10-13 104 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