Greetings
I need to display images stored in BLOB database fields in a LISTVIEW or similar widget from Yii2
I have the actionCreate that saves the image in table named honra as a BLOB
public function actionCreate()
{
$model = new Honra();
if ($model->load(Yii::$app->request->post()) && $model->save()) {
$file = UploadedFile::getInstance($model,'binaryfile');
$model->binaryfile=$file;
return $this->redirect(['view', 'id' => $model->id]);
} else {
return $this->render('create', [
'model' => $model,
]);
}
}
I also have the model named Honra
class Honra extends \yii\db\ActiveRecord{
public static function tableName()
{
return 'honra';
}
/**
* @inheritdoc
*/
public function rules()
{
return [
[['nome', 'texto'], 'required'],
[['texto', 'binaryfile'], 'string'],
[['ativo'], 'integer'],
[['nome'], 'string', 'max' => 255],
[['fileName'], 'string', 'max' => 100],
[['fileType'], 'string', 'max' => 50]
];
}
/**
* @inheritdoc
*/
public function attributeLabels()
{
return [
'id' => Yii::t('app', 'ID'),
'nome' => Yii::t('app', 'Nome'),
'texto' => Yii::t('app', 'Texto'),
'fileName' => Yii::t('app', 'File Name'),
'fileType' => Yii::t('app', 'File Type'),
'binaryfile' => Yii::t('app', 'Binaryfile'),
'ativo' => Yii::t('app', 'Ativo'),
];
}
}
The images are successfully stored in the table field named binaryfile
And i need to display each blob image stored in database in a view named view2 inside a LISTVIEW or similar widget
Anyone knows what block of code i need to put in view2 and the HonraController to achieve this ??
EDIT
SOLVED
HERE IS THE CODE THAT SOLVED THE ISSUE, NOW EVERYTHING IS WORKING WELL
public function actionCreate()
{
$model = new Honra();
if (Yii::$app->request->isPost) {
$model->file = UploadedFile::getInstance($model, 'file');
$filePath = 'uploadsimg/' . $model->file->baseName . '.' . $model->file->extension;
$model->fileName = $filePath;
$model->save();
$model->file->saveAs('uploadsimg/' . $model->file->baseName . '.' . $model->file->extension);
if ($model->load(Yii::$app->request->post()) && $model->save()) {
return $this->redirect(Url::toRoute(['create']));
}
}
return $this->render('create', ['model' => $model]);
}
MANY THANKS FOR THE HELP MIHAI
Here is the official documentation on Yii2 https://github.com/yiisoft/yii2/blob/master/docs/guide/input-file-upload.md It is quite simple.
some comments:
1) the pics might be private and it might be a big problem if somebody guesses the path of others. In this case you can put the file someplace NOT in the web folder. If you do this then you have to use the assets to show them assetManager->getPublishedUrl('@app/folder')?>/fav.ico
2) if the pics are not so sensitive then you can just save them someplace in the web folder.
As you can see you might have problems with uploading the same file name 2 times, as 1 will override the other. You can always rename the file, I would actually change their code to something like this
The shortest way I found to display a image from a BLOB field (in a MySQL database) was using this code (in my view):
Note that in this example I have only one row in the table 'TableName' as this example was used to retrieve the logo from the company. And there is only one company stored in that table.