将文件上载到文件夹并将名称保存到yii2中的数据库

时间:2017-03-29 11:15:34

标签: yii2

下午好! 有关于将文件上传到yii2的问题。其中有两个文件夹,用于存储原始图像和缩略图图像。在我处加载文件,但这里文件的名称没有加载到数据库中

模型     

namespace app\models;

use yii\base\Model;
use yii\db\ActiveRecord;
use yii\web\UploadedFile;

use yii\imagine\Image;
use Imagine\Image\Box;

/**
 * This is the model class for table "images".
 *
 * @property integer $id
 * @property string $original_image
 * @property string $prev_image
 */
class Images extends ActiveRecord
{

    public $imageFile;
    public $file_name;

    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'images';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['prev_image'], 'string', 'max' => 255],
            [['original_image'], 'string'],
            [['imageFile'], 'file', 'skipOnEmpty' => false, 'extensions' => 'png, jpg', 'maxSize' => 1024 * 1024 * 7],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'original_image' => 'Original Image',
            'prev_image' => 'Prev Image',
        ];
    }

    public function upload()
    {
        $temp=substr(md5(microtime() . rand(0, 9999)), 0, 20);

        if ($this->validate()) {
            $this->imageFile->saveAs('uploads/original/'.$temp.$this->imageFile->baseName . '.' . $this->imageFile->extension);
            $imagine = Image::getImagine();
            $image = $imagine->open('uploads/original/' . $temp.$this->imageFile);
            $image->resize(new Box(250, 150))->save('uploads/prev/' . $temp.$this
                    ->imageFile->baseName . '.' . $this->imageFile->extension, ['quality' => 70]);
            $this->file_name=$temp.$this->imageFile->baseName . '.' . $this->imageFile->extension;

            return true;
        } else {
            return false;
        }
    }
}

控制器         

namespace app\controllers;


use app\models\Images;
use Yii;
use yii\web\UploadedFile;

class ImageController extends \yii\web\Controller
{

        public function actionUpload()
    {
        $model = new Images();


        if ($model->load(Yii::$app->request->post())) {

            $model->imageFile = UploadedFile::getInstance($model, 'imageFile');
            $model->prev_image=$model->file_name;
            $model->original_image=$model->file_name;
            $model->save();

            if ($model->upload()) {
                return;
            }
        }

        return $this->render('upload', ['model' => $model]);
    }

}

如何在数据库中保存文件名的问题?提前谢谢

2 个答案:

答案 0 :(得分:1)

这是因为保存发生在uplaod操作之前,但您只在upload函数中定义了file_name。保存是将其保存到数据库中的内容。

控制器应如下所示:

namespace app\controllers;

use app\models\Images;
use Yii;
use yii\web\UploadedFile;

class ImageController extends \yii\web\Controller
{
    public function actionUpload(){

        $model = new Images();

        if ($model->load(Yii::$app->request->post())) {
            $uploadedFile = UploadedFile::getInstance($model, 'imageFile');
            $model->imageFile = $uploadedFile; 
            $model->prev_image = $uploadedFile->name 
            $model->original_image = $uploadedFile->name 
            $model->save();

            if ($model->upload()) {
                return;
            }
        }

        return $this->render('upload', ['model' => $model]);
    }
}

答案 1 :(得分:0)

现在谢谢,还有另外一个问题。我想重做整个代码。我为移动应用程序做api。但由于某种原因,我不会下载文件。我通过邮递员上传。我有一条消息:

[{"field":"imageFile","message":"Please upload a file."}]

<?php

namespace app\controllers;

use app\models\Images;
use Yii;
use yii\rest\ActiveController;
use yii\web\UploadedFile;

class ImageController extends ActiveController
{
public $modelClass = 'app\models\Images';


public function actions(){
    $actions=parent::actions();
    unset($actions['create']);

    return $actions;
}

public function actionCreate(){


    $model=new Images();

    if ($model->load(Yii::$app->request->post())) {

        $model->imageFile = UploadedFile::getInstance($model, 'imageFile');

        if ($model->upload()) {
            $model->original_image=$model->file_name;
            $model->prev_image=$model->file_name;

            $model->save(false);

            return;
        }
    }


}

}

<?php

namespace app\models;

use Yii;
use yii\web\UploadedFile;
use yii\imagine\Image;
use Imagine\Image\Box;
/**
* This is the model class for table "images".
*
* @property integer $id
* @property string $prev_image
* @property string $original_image
 */
class Images extends \yii\db\ActiveRecord
{
/**
 * @inheritdoc
 */

public $imageFile;
public $file_name;

public static function tableName()
{
    return 'images';
}

/**
 * @inheritdoc
 */
public function rules()
{
    return [

        [['prev_image', 'original_image'], 'string', 'max' => 255],
        [['imageFile'], 'file', 'skipOnEmpty' => false, 'extensions' => 'png, jpg', 'maxSize' => 1024 * 1024 * 10],
    ];
}

/**
 * @inheritdoc
 */
public function attributeLabels()
{
    return [
        'id' => 'ID',
        'prev_image' => 'Prev Image',
        'original_image' => 'Original Image',
    ];
}

public function upload()
{

    if ($this->validate()) {
        $temp=substr(md5(microtime() . rand(0, 9999)), 0, 20);
        $this->file_name=$temp.'_'.$this->imageFile->baseName . '.' . $this->imageFile->extension;
        $this->imageFile->saveAs('uploads/original/'.$this->file_name);
        $imagine = Image::getImagine();
        $image = $imagine->open('uploads/original/' . $this->file_name);
        $image->resize(new Box(250, 150))->save('uploads/prev/' . $this->file_name, ['quality' => 70]);

        return true;
    } else {
        return false;
    }
}

}  enter image description here