【问题标题】:Storing filestream to disk using PHP, MSSQL and FreeTDS使用 PHP、MSSQL 和 FreeTDS 将文件流存储到磁盘
【发布时间】: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


    【解决方案1】:

    这个限制可以在几个地方发挥作用,但我猜它可能在您的 FreeTDS 配置中(通常在 /etc/freetds.conf/etc/freetds/freetds.conf 中)。

    寻找类似这样的一行:

    text size = 64512
    

    并在[global] 部分将其更改为类似的内容:

    test size = 4294967295
    

    这应该可以解决问题 - 我相信 freetds.conf 中的默认设置是 Sybase 的旧版 [global] 设置。

    【讨论】:

      猜你喜欢
      • 2011-06-27
      • 2019-02-11
      • 1970-01-01
      • 1970-01-01
      • 2021-12-17
      • 1970-01-01
      • 1970-01-01
      • 2016-07-22
      • 2013-12-19
      相关资源
      最近更新 更多