Yii2显示存储在BLOB数据库字段中的图像 [英] Yii2 Display image stored in BLOB database field

查看:1186
本文介绍了Yii2显示存储在BLOB数据库字段中的图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问候

我需要显示LISTVIEW中存储在BLOB数据库字段中的图片或Yii2类似的小部件

我有一个actionCreate将图像保存在名为honra的表中作为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,
        ]);
    }
}

我也有名为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'),
    ];
}

}

图片已成功存储在名为binaryfile的表字段中

我需要在视图中显示存储在数据库中的每个blob图片命名为view2里面的LISTVIEW或类似的小部件

任何人都知道我需要放在view2和HonraController的代码块,

EDIT

这是解决问题的代码,现在一切正常工作

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]);
}

多少钱帮助MIHAI

推荐答案

以下是有关Yii2的官方文档 https://github.com/yiisoft/yii2/blob/master/docs/guide/input-file-upload.md
这很简单。

Here is the official documentation on Yii2 https://github.com/yiisoft/yii2/blob/master/docs/guide/input-file-upload.md It is quite simple.

一些注释:

1)图片可能是私有的,如果有人猜测他人的路径,这可能是一个大问题。在这种情况下,您可以将文件放在web文件夹中。如果你这样做,那么你必须使用资产显示它们assetManager-> getPublishedUrl('@ app / folder')?> / fav.ico

2)如果图片不那么敏感,那么你

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.

如您所见,您可能会遇到上传相同文件名2次的问题,因为1会覆盖其他文件。你可以随时重命名文件,我实际上会改变他们的代码到这样

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

        if ($model->file && $model->validate()) {
$model->file = UploadedFile::getInstance($model, 'file');
            $filePath = 'uploads/' . hash('ripemd160', microtime()) . $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']));
            }
        }

这篇关于Yii2显示存储在BLOB数据库字段中的图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆