【发布时间】:2017-04-21 13:49:21
【问题描述】:
我有一个 MSSQL 数据库,其中的文件存储为 blob,文件大小各不相同。我开发了一个概念证明并使用 PHP 的 SQLSRV 驱动程序,代码运行良好。我的客户要求应用程序在 Linux 环境中运行,所以我已经转移到 FreeTDS,我似乎遇到的唯一问题是我正在使用的 mssql_fetch_array 函数没有检索整个 blob。当我注意到存储在磁盘上的文件都是 63KB 时,我得出了这个结论,我将代码更改为 var_dump 保存二进制数据的变量,它显示 ["attachment"]=> string(64512) 与 blob 的大小无关(3,9mb in数据库)。
当我使用 SQLSRV 驱动程序时,我必须指定要检索的数据是二进制的,我没有遇到这些问题,但是,这始终失败。下面是我的代码摘录,请指出我的方法错误的地方。
$stmt3 = mssql_query($tsql3) or
die("<p style='color:red;'>{addFile} Could not $query: " . mssql_get_last_message() . "</p>");;
// handle the notices with attachments, add meta and save binary info to filesystem
while ( $noticeAttachmentList = mssql_fetch_array( $stmt3) ){
$c = $noticeAttachmentList[1]; // filename
$header = $noticeAttachmentList[2]; // file header
$download = $noticeAttachmentList[3]; // binary data
$nID = $noticeAttachmentList[4]; // FK for external system
// create the filename path & make the filename web-friendly
$filename = $uploadsDir;
$filename .= str_replace(' ', '_',$c);
// create entry in notice_attachement table
$noticeFileInfo = array(
'fk_notice_id' => $noticeID,
'filename' => $c,
'post_id' => $postID,
'notice_attachment_id' => $nID,
'date_added' => date("Y-m-d H:i:s", time())
);
// check if the file exists
if (file_exists($filename)){
echo "<p class='error'>Error: The file ".$filename." exists already in the destination folder.</p>";
return false;
}
// create the file
if (!file_exists($filename)){
echo "Your file, ". $filename ."is ready for download<br/>";
// download the file from the database and store in uploadsDir
file_put_contents($filename, $download);
//var_dump($download);
return true;
}
$file = $metaAttachmentPath . str_replace(' ', '_',$c);
}
【问题讨论】:
标签: php sql-server freetds