如何获取和管理数据库的流资源?PHP-DB2.


How can I get and manage a stream resource of DB? PHP-DB2

我正在使用DB2,PHP和Yii框架管理CLOB和BLOB类型(尽管我只使用php)。我要将文件上传到 DB2。使用 CLOB 和 BLOB 类型。但是我有一个问题,我无法下载文件。我使用以下代码获取资源,流类型:

if(is_resource($model->file_content)) echo 'resource of type:';
echo '<br>';
echo get_resource_type($model->file_content); 

我需要将此流转换为文件以在网络上下载。

我试过:print_r( stream_get_contents($model->file_content));

并使用fread()

是否有任何 php 函数来获取流类型?

编辑:类型是"ibm pdo Lob 流",它与stream_get_meta_data($model->file_content);一起得到

另外,我尝试过:

header('Pragma: public');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Content-Transfer-Encoding: binary');  
header('Content-length: '.$model->file_size);
header('Content-Type: '.$model->file_type);
header('Content-Disposition: attachment; filename='.$model->file_name);
echo $model->file_content;

但结果是一个包含我页面 html 的文件。

你可以帮我吗?谢谢

不需要编写纯PHP代码。 Yii有一个名为sendFile的方法来处理这个问题。看一看:

Yii::app()->request->sendFile('file.ext', "Content Of File", "Mime Time",TRUE);

正如官方文件所定义的那样sendFile()

向用户发送文件。

public void sendFile(string $fileName, string $content, string $mimeType=NULL, boolean $terminate=true)

也建议使用 Yii 的 CFileHelper 类。