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

495 views Asked by At

I am manage CLOB and BLOB types with DB2, PHP and Yii Framework (although I am using only php). I am getting to upload files to DB2. With CLOB and BLOB types. But I have a problem, I don't get to download files. I obtain a resource, stream type with this code:

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

I need to convert this stream in a file to download in a web.

I've tried: print_r( stream_get_contents($model->file_content));

and using fread()

Are there any php function to get the stream type?

Edit: The type is "ibm PDO Lob stream", It get with stream_get_meta_data($model->file_content);

Also, I've tried:

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;

But the result is a file with html of my page.

Can you help me? Thanks

1

There are 1 answers

2
Ali MasudianPour On

There is no need to write pure PHP code. Yii has a method called sendFile which handles this. Take a look:

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

As official document defines sendFile():

Sends a file to user.

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

It is also suggested to use Yii's CFileHelper class too.