vnagar
(Sams3001)
September 26, 2010, 1:58am
1
Hi,
I am new to yii. I have requirement to upload a file to database & then download it from database. I have browsed topics where they talk about uploading file to server using CUploadedFile.
Can anyone please let me know if I can save uploaded file to the database instead of saving on the server file system. Are there any APIs for this in Yii?
User should also be able to download the file from the database.
Any help would be greatly appreciated.
Thanks,
VJ
pangjanne
(Pngjanne)
September 26, 2010, 3:06am
2
May be, you need a blob datatype.
upload:
$file = /path/to/yourtmpfile;
$fp = fopen($file, 'rb');
$binary_data = fread($fp, filesize($file))
insert $binary_data to database;
download:
$binary_data = read from database blob field;
$fp = fopen($filename, 'wb');
fwrtie($fp, $binary_data);
download refer header() funciton;
http://php.net/manual/en/function.header.php
[poor english]
vnagar
(Sams3001)
September 26, 2010, 5:25am
3
Thanks for the reply. Is there a ready API for this in Yii Framework.
zaccaria
(Matteo Falsitta)
September 27, 2010, 10:55am
4
You don’t need any api.
you have simply to creata a blob fild in your table and save the data in it.
For example, assuming you have a table called foo with a blob field called binary_data:
$foo=new Foo;
$foo->binary_data=$binary_data
$foo->save();
The variable $binary_data can be filled with CUploadedFile.
For serve this data to the user, you can do an action download, for instance, like that:
public function actionDownload()
{
$model=$this->loadModel();
header('Content-Type: image/jpeg');
echo $model->data;
}
vnagar
(Sams3001)
September 27, 2010, 6:56pm
5
Thanks for your inputs. Shall try this.