Heim > Backend-Entwicklung > PHP-Tutorial > Yii如何使用model来上传多个文件

Yii如何使用model来上传多个文件

WBOY
Freigeben: 2016-06-06 20:45:14
Original
1036 Leute haben es durchsucht

我以前是使用CFormModel来上传图片(单张)。比如RegisterForm来注册用户上传照片并使用缩略图:

<code class="lang-php"><br><?php class RegisterForm extends CFormModel
{
    public $email;
    public $password;
    public $gender;
    public $homeland;
    public $iemi;
    public $registerDate;
    public $wentWhere;
    public $birthday;
    public $avatar; //头像,其最终结果应该是存储的Url
    public $thumb;//缩略图
    private $_identity;
    public function rules()
    {
        return array(

            array('email', 'required'),
            array('email','email','message'=>'email的格式不合法'),
            array('password', 'required'),
            array('gender','in','range'=>array(0,1)),
            array('homeland','required','message'=>'家乡必填,而且不容易更改'),
            array ('homeland','validateHome'),
            array('imei','required','message'=>'imei必填'),
            array('birthday','required','message'=>'birthday必填'),
            array('avatar','file','message'=>'必须设置一个头像')
        );
    }

    /**
     * Declares attribute labels.
     */
    public function attributeLabels()
    {
        return array(
            'email'=>'用户名/邮箱/手机号/漫游号',
            'password'=>'密码',
            'gender'=>'性别',
            ''
        );
    }

    /**
     * Authenticates the password.
     * This is the 'authenticate' validator as declared in rules().
     */
    public function authenticate($attribute,$params)
    {
        if(!$this->hasErrors())
        {
            $this->_identity=new UserIdentity($this->username,$this->password);
            if(!$this->_identity->authenticate())
                $this->addError('password','Incorrect username or password.');
        }
    }

    /**
     *     验证家乡是否合法。扩展到地级市
     *
     *
     */
    public   function validateHome(){

          $this->homeland;


          $this->addError('homeland','家乡不合法啊');



            }
    /**
     * Logs in the user using the given username and password in the model.
     * @return boolean whether login is successful
     */
    public function login()
    {
        if($this->_identity===null)
        {
            $this->_identity=new UserIdentity($this->username,$this->password);


            $this->_identity->setPersistentStates(array());
            $this->_identity->authenticate();
        }
        if($this->_identity->errorCode===UserIdentity::ERROR_NONE)
        {
            $duration=3600*24*10; // 10 days

            Yii::app()->user->login($this->_identity,$duration);
            return true;
        }
        else
            return false;
    }
}
</code>
Nach dem Login kopieren
Nach dem Login kopieren

然后在Controller里面:

<code class="lang-php">public function  actionRegister()
    {
        $registerForm = new RegisterForm();
        if (isset($_POST['RegisterForm'])) {
            $registerForm->attributes = $_POST['RegisterForm'];
            $registerForm->avatar = CUploadedFile::getInstance($registerForm, 'avatar');
            if ($registerForm->avatar) {
                $preRand = time() . mt_rand(0, 99999);
                $imageName='img_big'.$preRand.$registerForm->avatar->extensionName;
                $registerForm->avatar->saveAs('uploads/' . $imageName);
                $registerForm->avatar = $imageName;
            }
            $path = dirname(Yii::app()->BasePath) . '/uploads/';
            $thumb = Yii::app()->thumb; //与 $thumb=new Cthumb()有什么区别?
            $thumb->image = $path . 'img_small' . $preRand . $registerForm->avatar->extensionName;
            $thumb->width = 130;
            $thumb->height = 95;
            $thumb->mode = 4;
            $thumb->directory = $path;
            $thumb->defaultName = $preRand;
            $thumb->createThumb();
            $thumb->save();
            $registerForm->thumb = $thumb->image;
            $registerForm->registerDate = time();
//save方法会自动验证
            if ($registerForm->save() && $registerForm->login()) {
                Yii::app()->db->getLastInsertID(); //取得插入的Id。但是
            }

        }
</code>
Nach dem Login kopieren
Nach dem Login kopieren

上传的时候主要是这里:

<code class="lang-php">$registerForm->avatar = CUploadedFile::getInstance($registerForm, 'avatar');
</code>
Nach dem Login kopieren
Nach dem Login kopieren

但是现在遇到了一个问题。就是$registerForm继承了CFormModel,这个save方法是CActiveRecord的,为什么save方法就调用了呢?

此外,如果我要多文件上传,是不是

<code class="lang-php">$file1=CUploadedFile::getInstance($registerForm, 'file1');
$file2=CUploadedFile::getInstance($registerForm, 'file2');
</code>
Nach dem Login kopieren
Nach dem Login kopieren

就可以了?

回复内容:

我以前是使用CFormModel来上传图片(单张)。比如RegisterForm来注册用户上传照片并使用缩略图:

<code class="lang-php"><br><?php class RegisterForm extends CFormModel
{
    public $email;
    public $password;
    public $gender;
    public $homeland;
    public $iemi;
    public $registerDate;
    public $wentWhere;
    public $birthday;
    public $avatar; //头像,其最终结果应该是存储的Url
    public $thumb;//缩略图
    private $_identity;
    public function rules()
    {
        return array(

            array('email', 'required'),
            array('email','email','message'=>'email的格式不合法'),
            array('password', 'required'),
            array('gender','in','range'=>array(0,1)),
            array('homeland','required','message'=>'家乡必填,而且不容易更改'),
            array ('homeland','validateHome'),
            array('imei','required','message'=>'imei必填'),
            array('birthday','required','message'=>'birthday必填'),
            array('avatar','file','message'=>'必须设置一个头像')
        );
    }

    /**
     * Declares attribute labels.
     */
    public function attributeLabels()
    {
        return array(
            'email'=>'用户名/邮箱/手机号/漫游号',
            'password'=>'密码',
            'gender'=>'性别',
            ''
        );
    }

    /**
     * Authenticates the password.
     * This is the 'authenticate' validator as declared in rules().
     */
    public function authenticate($attribute,$params)
    {
        if(!$this->hasErrors())
        {
            $this->_identity=new UserIdentity($this->username,$this->password);
            if(!$this->_identity->authenticate())
                $this->addError('password','Incorrect username or password.');
        }
    }

    /**
     *     验证家乡是否合法。扩展到地级市
     *
     *
     */
    public   function validateHome(){

          $this->homeland;


          $this->addError('homeland','家乡不合法啊');



            }
    /**
     * Logs in the user using the given username and password in the model.
     * @return boolean whether login is successful
     */
    public function login()
    {
        if($this->_identity===null)
        {
            $this->_identity=new UserIdentity($this->username,$this->password);


            $this->_identity->setPersistentStates(array());
            $this->_identity->authenticate();
        }
        if($this->_identity->errorCode===UserIdentity::ERROR_NONE)
        {
            $duration=3600*24*10; // 10 days

            Yii::app()->user->login($this->_identity,$duration);
            return true;
        }
        else
            return false;
    }
}
</code>
Nach dem Login kopieren
Nach dem Login kopieren

然后在Controller里面:

<code class="lang-php">public function  actionRegister()
    {
        $registerForm = new RegisterForm();
        if (isset($_POST['RegisterForm'])) {
            $registerForm->attributes = $_POST['RegisterForm'];
            $registerForm->avatar = CUploadedFile::getInstance($registerForm, 'avatar');
            if ($registerForm->avatar) {
                $preRand = time() . mt_rand(0, 99999);
                $imageName='img_big'.$preRand.$registerForm->avatar->extensionName;
                $registerForm->avatar->saveAs('uploads/' . $imageName);
                $registerForm->avatar = $imageName;
            }
            $path = dirname(Yii::app()->BasePath) . '/uploads/';
            $thumb = Yii::app()->thumb; //与 $thumb=new Cthumb()有什么区别?
            $thumb->image = $path . 'img_small' . $preRand . $registerForm->avatar->extensionName;
            $thumb->width = 130;
            $thumb->height = 95;
            $thumb->mode = 4;
            $thumb->directory = $path;
            $thumb->defaultName = $preRand;
            $thumb->createThumb();
            $thumb->save();
            $registerForm->thumb = $thumb->image;
            $registerForm->registerDate = time();
//save方法会自动验证
            if ($registerForm->save() && $registerForm->login()) {
                Yii::app()->db->getLastInsertID(); //取得插入的Id。但是
            }

        }
</code>
Nach dem Login kopieren
Nach dem Login kopieren

上传的时候主要是这里:

<code class="lang-php">$registerForm->avatar = CUploadedFile::getInstance($registerForm, 'avatar');
</code>
Nach dem Login kopieren
Nach dem Login kopieren

但是现在遇到了一个问题。就是$registerForm继承了CFormModel,这个save方法是CActiveRecord的,为什么save方法就调用了呢?

此外,如果我要多文件上传,是不是

<code class="lang-php">$file1=CUploadedFile::getInstance($registerForm, 'file1');
$file2=CUploadedFile::getInstance($registerForm, 'file2');
</code>
Nach dem Login kopieren
Nach dem Login kopieren

就可以了?

你好啊。这个多文件上传的这问题你解决了吗?

Verwandte Etiketten:
Quelle:php.cn
Erklärung dieser Website
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn
Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage