Download link from Database BLOB with yii Framewor

2019-09-06 06:01发布

问题:

I'm totaly new to the Yii Framework and i don't understand the how to create a download link with a popup window that asks if you whant to save the file.

What i know and have till now:

  1. I know how the CHtml::link(...) works and already impelemted that.
  2. I extracted the Blob content form the db and saved it in a $data->file
  3. The link where i whant to click should be inside a CListView

My database USERZ looks like this

+---------+-------------+------+-----+---------+----------------+
| Field   | Type        | Null | Key | Default | Extra          |
+---------+-------------+------+-----+---------+----------------+
| id      | int(11)     | NO   | PRI | NULL    | auto_increment |
| file    | blob        | NO   |     | NULL    |                |
| USER_id | int(11)     | NO   | MUL | NULL    |                |
| zname   | varchar(40) | NO   |     | NULL    |                |
| date    | date        | NO   |     | NULL    |                |
+---------+-------------+------+-----+---------+----------------+

my Controller funktion (SiteController.php)

public function actionVerwal()
{
    $model2=new USERZ;
    $this->render('verwal',array('model2'=>$model2));

}

and the view (verwal.php)

<?php
 $this->widget('zii.widgets.CListView', array(
    'dataProvider'=>$model2->a(Yii::app()->user->getId()),
    'itemView'=>'_verwal',
));

//function a($id) just makes sure that the right user data is provided

The Part that i dont understand (_verwal.php):

<b><?php echo CHtml::encode($data->getAttributeLabel('Files')); ?>:</b>
<?php echo CHtml::link(CHtml::encode($data->zname), $data->file); ?>
<br />

How can i make the File name into a Link so it provides a download. I have all it needs the Content of the File and the Name but i dont know how to put them together and make them into 1 download file popup.

回答1:

File downloads are handled by a separate request to your application that results in the file headers & info being sent.

So what you want to do is it to create a new action in your controller called something like public function actionDownload($id). Then in your view you will link to that action.

<?php echo CHtml::link(CHtml::encode($data->zname), array('site/download', 'id' => $data->id)); ?>

We send the id of the data so the action knows what model it's working with.

$user = USERZ::model()->findByPk($id);

Now you can send the users file to them by sending the correct headers within your download action.

header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename=MY_FILE_NAME');
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
header('Content-Length: ' . count($user->file));
ob_start();
echo $user->file;
exit;

You shouldn't really be storing your files in the database though, consider storing them in a folder like protected/files.



标签: php yii