Reputation: 475
i am new in yii and i use to upload images in databse but images are stored in folder but not saved in database what is the problem with this code coud not undurstand
public function actionCreate()
{
$model = new Jobs;
// $site_url=Yii::$app->getUrlManager()->createAbsoluteUrl('');
if ($model->load(Yii::$app->request->post()) && $model->save()) {
$image = UploadedFile::getInstance($model,'image');
$imagepath='upload/jobs/';
$path=$imagepath.rand(10,100).$image->name;
if(!empty($image)){
$image->saveAs($path);
$model->image=$image->name;
$model->save();
}
return $this->redirect(['view', 'id' => $model->id]);
}
else {
// echo "file not ulpoaded";
}
return $this->render('create', [
'model' => $model,
]);
}
model is here named as Jobs.php.
namespace app\models;
use Yii;
use yii\web\UploadedFile;
use yii\base\Model;
/**
* This is the model class for table "jobs".
*
* @property integer $id
* @property integer $users_id
* @property string $title
* @property string $deadline
* @property string $urgency
*/
class Jobs extends \yii\db\ActiveRecord
{
/**
* @inheritdoc
*
*
*/
public $image;
public static function tableName()
{
return 'jobs';
}
/**
* @inheritdoc
*/
public function rules()
{
return [
[['users_id', 'title', 'deadline', 'urgency'], 'required'],
[['users_id'], 'integer'],
[['content'], 'string'],
[['deadline'], 'required'],
[['urgency'], 'string'],
[['urgency'], 'safe'],
[['image'],'file', 'skipOnEmpty'=>true,'extensions' => 'png,gif,jpg'],
[['title'], 'string', 'max' => 255]
];
}
/**
* @inheritdoc
*/
public function attributeLabels()
{
return [
'id' => Yii::t('app', 'ID'),
'users_id' => Yii::t('app', 'Users ID'),
'content'=>Yii::t('app','Content'),
'title' => Yii::t('app', 'Title'),
'deadline' => Yii::t('app', 'Deadline'),
'urgency' => Yii::t('app', 'Urgency'),
'image' => Yii::t('app', 'image'),
];
}
}
the view file is here named as (_form.php)
use yii\helpers\Html;
use yii\widgets\ActiveForm;
use yii\web\UploadedFile;
//use kartik\widgets\FileInput;
/* @var $this yii\web\View */
/* @var $model app\models\Jobs */
/* @var $form yii\widgets\ActiveForm */
?>
<div class="jobs-form">
<?php
$form = ActiveForm::begin([
'options' => ['enctype' => 'multipart/form-data']]); ?>
<?= $form->field($model, 'users_id')->textInput() ?>
<?= $form->field($model, 'content')->textInput(['maxlength' => true]) ?>
<?= $form->field($model, 'title')->textInput(['maxlength' => true]) ?>
<?= $form->field($model, 'deadline')->textInput() ?>
<?= $form->field($model, 'urgency')->dropDownList([ 'No', 'yes', ], ['prompt' => '']) ?>
<?= $form->field($model, 'image')->fileInput() ?>
<div class="form-group">
<?= Html::submitButton($model->isNewRecord ? Yii::t('app', 'Create') : Yii::t('app', 'Update'), ['class' => $model->isNewRecord ? 'btn btn-success' : 'btn btn-primary']) ?>
</div>
<?php ActiveForm::end(); ?>
</div>
enter code here
plzz help me to upload the image in database i dont know what the problem with this code.
Upvotes: 2
Views: 2670
Reputation: 920
change your upload code like below:
if ($model->load(Yii::$app->request->post()) && $model->save()) {
$image = UploadedFile::getInstance($model,'image');
$imagepath='upload/jobs/';
$rand_name=rand(10,100);
if ($image)
{
$model->image = "category_{$rand_name}-{$image}";
}
if($model->save()):
if($image):
$image->saveAs($imagepath.$model->image);
endif;
endif;
return $this->redirect(['view', 'id' => $model->id]);
}
Upvotes: 2
Reputation: 3567
You're mixing image name and image file in your code.
public $image
is unnecessary if image
is a field in your db-table. Instead add public $imageFile
in your model, and update your form and controller accordingly.
Remember, $this->image
, or $model->image
is the name of the image as a string, you're mixing in an upload object. Instead create another variable so you can store the uploaded image in, and use the image variable for saving the actual name for later reference.
See if you can figure it out, and update your question if you can't get it to work.
http://www.yiiframework.com/doc-2.0/guide-input-file-upload.html
Upvotes: -1