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 关于#qt#的问题:Qt代码的移植问题
  • ¥50 求图像处理的matlab方案
  • ¥50 winform中使用edge的Kiosk模式
  • ¥15 关于#python#的问题:功能监听网页
  • ¥15 怎么让wx群机器人发送音乐
  • ¥15 fesafe材料库问题
  • ¥35 beats蓝牙耳机怎么查看日志
  • ¥15 Fluent齿轮搅油
  • ¥15 八爪鱼爬数据为什么自己停了
  • ¥15 交替优化波束形成和ris反射角使保密速率最大化