【问题标题】:Download Blob file with php ( mysql )使用 php ( mysql ) 下载 Blob 文件
【发布时间】:2015-12-12 21:39:45
【问题描述】:

我有一个 MySQL 数据库,其中有一个表调用,例如“ALL”可以包含一个 blob 文件。

在表中,我存储了:文件大小、类型、名称和内容。

当我尝试下载 blob 文件时出现问题。

这是我的脚本:

<?php
$connection =  mysqli_connect("localhost","user","password",dbname)
or die('Database Connection Failed');
mysqli_set_charset($connection,'utf-8');

$query = "SELECT * " ."FROM ALL WHERE id = '25'";
$result = mysqli_query($connection,$query) 
or die('Error, query failed');

 list($id, $user_made, $title, $category, $sub_category, $text, $type, $date, $time, $namefile1, $typefile1, $sizefile1, $contentfile1, $namefile2, $typefile2, $sizefile2, $contentfile2, $namefile3, $typefile3, $sizefile3, $contentfile3) = mysqli_fetch_array($result);
 echo $id . $namefile1 . $typefile1 . $sizefile1;
 header('Content-Length: '.$sizefile1);
 header('Content-Type: '.$typefile1);
 header('Content-Disposition: attachment; filename="'.$namefile1.'"');
 header('Content-Transfer-Encoding: binary');
 header('Cache-Control: must-revalidate, post-check=0, pre-check=0');  
 ob_clean();
 flush();
 // echo $contentfile1;
 mysqli_close($connection);
 exit;
?>

但页面只输出“$contentfile1”和“$id . $namefile1 . $typefile1 . $sizefile1”的回显。

每个人都可以帮助我吗? 谢谢

【问题讨论】:

  • 您需要回显文件的内容,而不是 id 或其他信息。您需要设置到标题中的其他数据,如文件大小(内容长度)、文件类型、文件名等
  • 感谢 Aleksey,我已经解决了我的问题!再次感谢

标签: php mysql file blob


【解决方案1】:

在 ob_clean 之前添加此代码。

    $context = stream_context_create();
    $file = fopen($filename, 'rb', FALSE, $context);
    while(!feof($file))
    {
       echo stream_get_contents($file, max_bytes_to_read);
    }
    fclose($file);

您可以在http://php.net/manual/en/function.stream-get-contents.php获取有关stream_get_contents的更多信息

【讨论】:

    猜你喜欢
    • 2012-05-19
    • 2015-11-10
    • 2011-03-27
    • 2015-06-20
    • 1970-01-01
    • 2011-12-18
    • 2013-03-31
    • 2013-02-07
    • 2014-03-28
    相关资源
    最近更新 更多