I have a task to create company profile of company where i work. Then i decide using Yii framework to develop that. One of the fitur is news and it needs upload image to complete the news. I write in here just because i can open it again in the future. i realize that this way to upload is simpler than php non framework. here we go :

in view -> form, we have :

    $form = $this->beginWidget('CActiveForm', array(
        'id' => 'news-form',
        'enableAjaxValidation' => false,

        'htmlOptions' => array('enctype' => 'multipart/form-data')
            ));
    ?>



        labelEx($model, 'newsImage'); ?>
       
        error($model, 'newsImage'); ?>
   

endWidget(); ?>

in models, we have :

class News extends CActiveRecord
{
    public $newsImage;


    //other function

    public function rules()
    {       
        return array(          
            array('newsId', 'numerical', 'integerOnly'=>true),
            array('newsTitle,  newsAuthor', 'length', 'max'=>200),
            array('newsImage', 'file', 'types'=>'jpg, gif, png'),
            array('user_in, user_up', 'length', 'max'=>8),
            array('newsDate, newsContent, date_in, date_up', 'safe'),
            array('newsId, newsTitle, newsDate, newsContent, newsImage, newsAuthor, user_in, date_in, user_up, date_up', 'safe', 'on'=>'search'),
        );
    }

    
    //other function
}

in controller we have save create example :

public function actionCreate() {
        $model = new News;
        if (isset($_POST['News'])) {
            $model->attributes = $_POST['News'];
            $model->newsImage = CUploadedFile::getInstance($model, 'newsImage');
            if ($model->save()) {
                $model->newsImage->saveAs(Yii::app()->basePath.'/../images/news/'.$model->newsImage);
                $this->redirect(array('view', 'id' => $model->newsId));
            }
        }

        $this->render('create', array(
            'model' => $model,
        ));
    }


I hope this help. I get reference from here :
http://www.yiiframework.com/wiki/2/how-to-upload-a-file-using-a-model/


This entry was posted on 4/26/2013 02:37:00 AM and is filed under . You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

7 komentar:

    nitin rawat said...

    how will you call the individual image on view page

  1. ... on April 25, 2014 at 12:19 AM  
  2. arum said...

    try this one :
    "< style="float:left; margin: 10px 10px 10px 0px;" src="request->baseUrl; ?>/images/news/newsImage); ?>" height="80" width="100" />"

    I use html code.

  3. ... on April 25, 2014 at 3:10 AM  
  4. arum said...

    before "style" word is "img"

  5. ... on April 25, 2014 at 3:10 AM  
  6. nitin rawat said...

    image file name should be with respect to the id number.
    by using your syntax it is showing error. as it is not getting the file name.

    Here, the file uploaded should name w.r.t the id number. so that we can call the specific id image only.

    kindly let me know how we can do this..
    i am newbie to yii.. plz help. Thanks in advance

  7. ... on April 28, 2014 at 12:28 AM  
  8. nitin rawat said...

    i have managed to save file by id like 1.jpg, 2.jpg, etc (w.r.t "id")
    and also able to call on view page by
    ""

    but here as you can see.. .jpg extension is added manually
    so it will only work for .jpg files
    we need to rename the whole file name during uploading (assigned by framework) and by this we call it by the same name on view page.

    Thanks in advance.

  9. ... on April 28, 2014 at 1:04 AM  
  10. nitin rawat said...

    "< img src="basePath.'?> /../images/id.'.jpg' ?>" height="400" >"

  11. ... on April 28, 2014 at 1:08 AM  
  12. arum said...

    When I store the picture, i don't rename the file (add ".jpg"). By default, it will store as the original name, for example : fish.jpg.
    You can save the path of file too if you dont wanna type the path.

  13. ... on April 28, 2014 at 11:12 PM