Yii , saving an image from $_FILES , but without using models

本秂侑毒 提交于 2019-12-09 17:51:43

问题


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:


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)


来源:https://stackoverflow.com/questions/10584908/yii-saving-an-image-from-files-but-without-using-models

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!