donglu7816 2016-12-07 07:49
浏览 25
已采纳

文件名被脚本缩短

I have some php code that connects to a MSSQL, looks up some values and retrieves a file that is stored in binary format. The table has the filename, header and the binary data. Presently my code is downloading the file as expected however, if there is a space in the filename, the name is cut short, as if the space is acting as a delimeter. e.g. value in db "This is an example file.pdf" outputted filename "This" and the browser recognises the pdf type.

My somewhat functional code is below:

/* Retrieve and display the data.  
        The return data is retrieved as a binary stream. */  
        if ( sqlsrv_fetch( $stmt ) )  
        {  
            $noticeID = sqlsrv_get_field( $stmt, 0, SQLSRV_PHPTYPE_INT);
            $filename = sqlsrv_get_field( $stmt, 1, SQLSRV_PHPTYPE_STRING("UTF-8"));
            $header = sqlsrv_get_field( $stmt, 2, SQLSRV_PHPTYPE_STRING(SQLSRV_ENC_BINARY));
            $download = sqlsrv_get_field( $stmt, 3, SQLSRV_PHPTYPE_STREAM(SQLSRV_ENC_BINARY));  
            header('Content-Type: '.$header);  
            header("Content-Disposition: attachment; filename=$filename");
            echo "Your file, $filename is ready for download";
            fpassthru($download);  
        }  
        else  
        {  
             echo "Error in retrieving data.</br>";  
             print_r( sqlsrv_errors(), true);  
        } 

I suspect that the issue is that the filename is stored as a VARCHAR(255) in the DB and somehow is breaking the "Content-Disposition" header line. Research brought me to this page but it isn't very clear on how to handle VARCHAR and when I try and "hack" it by guessing (see below) and get a filename "index.php" (although it does download the file)

$filename = sqlsrv_get_field( $stmt, 1, SQLSRV_PHPTYPE_VARCHAR);

I've also tried this in place of the faulty header line:

$filename = sqlsrv_get_field( $stmt, 1, SQLSRV_PHPTYPE_STREAM(SQLSRV_ENC_BINARY)))

It still cuts off the filename. If there is no space in the filename, the code executes perfectly.

BONUS: does anyone know how I could save the file to the filesystem rather than having a download dialog? This code is proof of concept for a data migration script I'm writing and that would also be very helpful :)

  • 写回答

1条回答 默认 最新

  • dongyanju0945 2016-12-07 10:41
    关注

    Please try the following: concatenate the header as follows:

    header('Content-Disposition: attachment; filename="'."$filename".'"');
    

    And save the file via:

    function file_put_contents()
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?