在Yii框架中如何在上传前重命名图像

在Yii框架中如何在上传前重命名图像,yii,Yii,我已跟随yii网站使用上传图像,代码如下: class ItemController extends CController { public function actionCreate() { $model=new Item; if(isset($_POST['Item'])) { $model->attributes=$_POST['Item']; $model->im

我已跟随yii网站使用上传图像,代码如下:

class ItemController extends CController
{
    public function actionCreate()
    {
        $model=new Item;
        if(isset($_POST['Item']))
        {
            $model->attributes=$_POST['Item'];
            $model->image=CUploadedFile::getInstance($model,'image');
            if($model->save())
            {
                $model->image->saveAs('path/to/localFile');
                // redirect to success page
            }
        }
        $this->render('create', array('model'=>$model));
    }
}
然而,我如何通过currentdate+filename.png重命名文件并上传到path,我还需要更新和删除的代码。 多谢各位

我已经解决了这个问题:

public function currentDate(){
        $date = date('m-d-Y-h-i-s', time());
        return $date;
    }

public function actionCreate(){
        $model = new News();

        if(isset($_POST['News']))
        {
            $model->attributes=$_POST['News'];          
            $uploadedFile = CUploadedFile::getInstance($model, 'images');
            $fileName = "{$this->currentDate()}-{$uploadedFile}";
            $model->images = $fileName;         
            if($model->save()){ 
              $uploadedFile->saveAs("upload/".$fileName);
              $this->redirect(array('news/index'));
            }else{
                $model = new News();
                $this->render('create',
                            array('model' =>$model,
                            'result'=>'insert new fail !',
                )); 
            }
        }else{
            $this->render('create',
            array(
                'model'=>$model,
            ));
        }
    }

要在数据库中上载和更新后重命名文件,请尝试以下代码

    $model=new Item;
    if(isset($_POST['Item']))
    {
        $model->attributes=$_POST['Item'];

        if($model->save())
        {
            $imageName = @$_FILES["MenuItems"]["name"]["image"];
            $uniqueName = (imageName . $model->id) . '.' . (end(explode('.', $imageName)));
            $model->image=CUploadedFile::getInstance($model,'image');
            $model->image->saveAs('path/to/localFile/'.$uniqueName);
            $model->image = $uniqueName;
            $model->save();
            // redirect to success page
        }
    }
    $this->render('create', array('model'=>$model));

要在数据库中上载和更新后重命名文件,请尝试以下代码

    $model=new Item;
    if(isset($_POST['Item']))
    {
        $model->attributes=$_POST['Item'];

        if($model->save())
        {
            $imageName = @$_FILES["MenuItems"]["name"]["image"];
            $uniqueName = (imageName . $model->id) . '.' . (end(explode('.', $imageName)));
            $model->image=CUploadedFile::getInstance($model,'image');
            $model->image->saveAs('path/to/localFile/'.$uniqueName);
            $model->image = $uniqueName;
            $model->save();
            // redirect to success page
        }
    }
    $this->render('create', array('model'=>$model));

您可以使用我稍后创建的此方法上载文件,并在上载之前更改其名称:

public static function createAttach($model, $imageAttrName) {
    $model->$imageAttrName = CUploadedFile::getInstance($model, $imageAttrName);
    $fecha = date('YmdHms');

    if ($model->$imageAttrName) {
        $attach_src = Yii::app()->basePath . '/../upload/' . $fecha.'.'.$model->$imageAttrName->getExtensionName(); //. '_' . $model->$imageAttrName;
        $model->$imageAttrName->saveAs($attach_src);
        $model->$imageAttrName = $fecha.'.'.$model->$imageAttrName->getExtensionName();// . '_' . $model->$imageAttrName;
    }
}

您可以使用我稍后创建的此方法上载文件,并在上载之前更改其名称:

public static function createAttach($model, $imageAttrName) {
    $model->$imageAttrName = CUploadedFile::getInstance($model, $imageAttrName);
    $fecha = date('YmdHms');

    if ($model->$imageAttrName) {
        $attach_src = Yii::app()->basePath . '/../upload/' . $fecha.'.'.$model->$imageAttrName->getExtensionName(); //. '_' . $model->$imageAttrName;
        $model->$imageAttrName->saveAs($attach_src);
        $model->$imageAttrName = $fecha.'.'.$model->$imageAttrName->getExtensionName();// . '_' . $model->$imageAttrName;
    }
}

检查这个链接:问题本身你有答案。我的问题是当我们上传时如何重命名文件,例如文件命名为test.png,当我上传到文件夹时,我希望文件夹中的文件命名为naming currentdate-test.png,谢谢你的回答现在我对这个问题有了自己的解决方案。检查这个链接:问题本身你有答案。我的问题是当我们上传时如何重命名文件,例如文件名为naming test.png,当我上传到文件夹时,我希望文件夹中的文件名为naming currentdate-test.png,谢谢你的回答现在我对这个问题有了自己的解决办法。什么是你前进的方式?什么是你前进的方式?