dongxing2692 2015-09-14 14:41
浏览 120
已采纳

在PHP Mysql中下载完整大小的文件

I have a problem downloading the Blob file from MYSQL using php.

Here is my php script in downloading file:

        $querysel = "Select * from file where file_id = '$fileid'";
        $resultsel = mysql_query($querysel);
        $rowsel = mysql_fetch_array($resultsel);

        $filename = $rowsel['file_name'];
        $mimetype = $rowsel['mime_type'];
        $filesize = $rowsel['file_size'];

        header("Content-length: ".$filesize);
        header("Content-type: ".$mimetype);
        header("Content-disposition: attachment; filename=".$filename);
        header("Content-Description: PHP Generated Data");
        header("Content-transfer-encoding: binary");
        echo $filename;

And this table in mysql:enter image description here

PROBLEM   [UPDATED]

  1. What seems to be the problem here is that after downloading the file, the file size from that particular file don't match with the file size saved in mysql. I don't know why?

  2. I also encounter this small problem, After I downloaded the Powerpoint and delete it and download it again would be automatically saved to downloads and will automatically be opened. Is that normal?

Example:

The BLOB file with 6.8MB of file size was supposed to be downloaded with 6.8MB also but what happened here is that the file size of the downloaded file is only 25bytes. Why?

Any help would be much appreciated. Thanks

  • 写回答

1条回答 默认 最新

  • dsk920417 2015-09-14 15:13
    关注

    It seems to me you want to be echoing the file contents and not its filename

        $querysel = "Select * from file where file_id = '$fileid'";
        $resultsel = mysql_query($querysel);
        $rowsel = mysql_fetch_array($resultsel);
    
        $filename = $rowsel['file_name'];
        $mimetype = $rowsel['mime_type'];
        $filesize = $rowsel['file_size'];
    
        header("Content-length: ".$filesize);
        header("Content-type: ".$mimetype);
        header("Content-disposition: attachment; filename=".$filename);
        header("Content-Description: PHP Generated Data");
        header("Content-transfer-encoding: binary");
    
        echo $rowsel['file_content'];          //<-- changed line
    

    Of course you still probably have a problem with the larger files in that a BLOB is not large enough to hold 6.8 Meg. See @Fred-ii- comment for details and links to all the relevant info you need to check that.

    Even a LONGBLOB is only 4,294,967,295 bytes, thats a little over 4Gig, so you probably need to do some size checking before storing to even a LONGBLOB.

    The safest suggestion would be to store the actual files on disk and only the filename and path in the database.

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

报告相同问题?

悬赏问题

  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘