Yii , saving an image from $_FILES , but without u

2019-04-07 17:19发布

Is this possible ? This is with a model

  CUploadedFile::getInstance($model,'newsimage');
  $model->image->saveAs("image\path")

but I don't want to create a model just so i can save my image.

What I actualy need this for is ... well I am trying to make the "Upload image" function of CKEditor to work, but I need a script for the image saving. When I click the "Upload image" button i just call an action and from there I have access to the picture I have selected, using $_FILES, but I can't seem to save the file to a target directory.

Is it possible to save the file to a target path ("C:\myProject\images" for example) and not use a model ?

EDIT :

Here is a solution i found a little bit later The file i upload is in $_FILES['upload'] so ..

$temp = CUploadedFile::getInstanceByName("upload");  // gets me the file into this variable (  i gues this wont work for multiple files at the same time )
$temp->saveAs("D:/games/" . $temp->name);  // full name , including the filename too.

1条回答
聊天终结者
2楼-- · 2019-04-07 17:36

Assuming that "without model"="without a db table"

You Just make a UploadForm.php extending from CFormModel in your models directory

class UploadForm extends CFormModel
{
    public $upload_file;

    public function rules()
    {
        return array(
        array('upload_file', 'file', 'types'=>'jpg,jpeg,gif,png','maxSize'=>10*1024*1024),
        );
    }

    /**
     * Declares attribute labels.
     */
    public function attributeLabels()
    {
        return array(
            'upload_file'=>'Upload File',
        );
    }

}

and in your controller

$model->upload_file=CUploadedFile::getInstance($model,'upload_file');
$model->upload_file->saveAs("C:\myProject\images\".$model->upload_file->name)
查看更多
登录 后发表回答