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.

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

报告相同问题?

悬赏问题

  • ¥20 matlab计算中误差
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料
  • ¥15 使用R语言marginaleffects包进行边际效应图绘制
  • ¥20 usb设备兼容性问题
  • ¥15 错误(10048): “调用exui内部功能”库命令的参数“参数4”不能接受空数据。怎么解决啊