Calcio
(Cálcio)
October 22, 2015, 4:26pm
1
Hi guys, I’m face a problem when I try do an upload and save in DB. The upload works fine but save in DB not.
I think it’s occur coz I put a "hash concatened with image name.
I followed the doc, but the full process do not work. I don’t know where is my mistake.
Follow my codes.
Model
public function upload()
{
if ($this->validate()) {
FileHelper::createDirectory('uploads/img/banners/');
$this->url_imagem->saveAs('uploads/img/banners/' . $this->url_imagem->baseName . '-' . date('YmdHis') . '.' . $this->url_imagem->extension);
return true;
} else {
return false;
}
}
Conreoller
public function actionCreate()
{
$model = new Banners();
if ($model->load(Yii::$app->request->post())) {
// && $model->save()
// return $this->redirect(['view', 'id' => $model->id]);
$model->url_imagem = UploadedFile::getInstance($model, 'url_imagem');
//$model->url_imagem = UploadedFile::getInstanceByName('url_imagem');
var_dump($model->url_imagem);
$model->upload();
} else {
return $this->render('create', [
'model' => $model,
]);
}
}
in database your storing image or Only file name if your storing file name then remove hash from the file name and rename it . or if you don’t want to do it then store image as it but while storing in data base rename it and save it .
After that when you download that than check the image name
for example : if your image name is #imagename you store in database as imagename, while downloading use strreplace(#imagename , #) and match with database name and download it.
I think it may work. I do same thing when i was downloading or uploading the image . I do not store image in database i only store file path , image name and mime type in data base.
So , i think it will help you.
Calcio
(Cálcio)
October 23, 2015, 10:35am
3
@Amitesh I want store only file path. My Problem is, I dont’n can save it.
I forgot to paste the error.
1. in /var/www/html/testes/Yii2/yiibrasil-cms/vendor/yiisoft/yii2/helpers/BaseFileHelper.php at line 147
138139140141142143144145146147148149150151152153154155156 if ($checkExtension) {
return static::getMimeTypeByExtension($file, $magicFile);
} else {
throw new InvalidConfigException('The fileinfo PHP extension is not installed.');
}
}
$info = finfo_open(FILEINFO_MIME_TYPE, $magicFile);
if ($info) {
$result = finfo_file($info, $file); //The error is here
finfo_close($info);
if ($result !== false) {
return $result;
}
}
return $checkExtension ? static::getMimeTypeByExtension($file, $magicFile) : null;
}
selvakumar
(Selvakumar Kaliyappan)
October 23, 2015, 2:34pm
4
This error is displayed because you don’t have php-devel package installed. Check your php.ini file
TO install the php-devel run the following code in centos
yum --enablerepo=remi,remi-php54 install php-devel or
To install the PHP extensions run pecl install php-devel
Refer : http://php.net/manual/en/fileinfo.installation.php