Yii Framework Forum: How To Upload Files with CUploadedFile class in CRUD interface - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

How To Upload Files with CUploadedFile class in CRUD interface guide to help that guys who are having trouble Rate Topic: -----

#1 User is offline   herohat 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 1
  • Joined: 07-October 11

Posted 09 October 2011 - 10:31 AM

THE CONTROLLER


-> function create:

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

		// Uncomment the following line if AJAX validation is needed
		 $this->performAjaxValidation($model);

		if(isset($_POST['Fotos2']))
		{
			$model->attributes=$_POST['Fotos2'];
                        $model->url=CUploadedFile::getInstance($model,'url');
                        $fecha = date('YmdHms');
                        $model->url->saveAs(Yii::app()->basePath.'/../images/uploads/'.$fecha.'_'.$model->url);
                        $model->url = $fecha.'_'.$model->url;                        
			if($model->save())
				$this->redirect(array('view','id'=>$model->id));
		}

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


->function update:

	public function actionUpdate($id)
	{
		$model=$this->loadModel($id);

		// Uncomment the following line if AJAX validation is needed
		// $this->performAjaxValidation($model);

		if(isset($_POST['Fotos2']))
		{
			$model->attributes=$_POST['Fotos2'];
                        
                        $model->url=CUploadedFile::getInstance($model,'url');
                        $fecha = date('YmdHms');                        
                        
                        if ( (is_object($model->url) && get_class($model->url)==='CUploadedFile')) {
                            $model->url->saveAs(Yii::app()->basePath.'/../images/uploads/'.$fecha.'_'.$model->url);                        
                            $model->url = $fecha.'_'.$model->url;
                        }
                                                                                                                            
			if($model->save())
				$this->redirect(array('view','id'=>$model->id));
		}

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



MODEL

Declare the public property url (the name of my upload file input)
class Fotos2 extends CActiveRecord
{
    
        public $url;
	/**
	 * Returns the static model of the specified AR class.
	 * @return Fotos2 the static model class
....


in the public function rules, add this array:

...
array('url', 'unsafe'), //in order to can update the record without upload the file again when is not necessary
array('url', 'file', 'types'=>'jpg, gif, png'),  //validate the file extension



THE VIEW _FORM

<div class="form">

<?php $form=$this->beginWidget('CActiveForm', array(
	'id'=>'fotos2-form',
	'enableAjaxValidation'=>true,
        'htmlOptions'=>array('enctype'=>'multipart/form-data'),
)); ?>

	<p class="note">Fields with <span class="required">*</span> are required.</p>

	<?php echo $form->errorSummary($model); ?>

	<div class="row">
		<?php echo $form->labelEx($model,'nombre'); ?>
		<?php echo $form->textField($model,'nombre',array('size'=>50,'maxlength'=>50)); ?>
		<?php echo $form->error($model,'nombre'); ?>
	</div>

	<div class="row">
		<?php echo $form->labelEx($model,'url'); ?>
		<?php echo $form->fileField($model,'url',array('size'=>60,'maxlength'=>255)); ?>
		<?php echo $form->error($model,'url'); ?>
	</div>

	<div class="row buttons">
		<?php echo CHtml::submitButton($model->isNewRecord ? 'Create' : 'Save'); ?>
	</div>

<?php $this->endWidget(); ?>

</div><!-- form -->


I hope this guide help son want who are getting error like "the filexx cannot be blank"
0

#2 User is offline   robregonm 

  • Expert Yii Developer
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 594
  • Joined: 30-July 09
  • Location:Colombia

Posted 09 October 2011 - 05:08 PM

Good!
You can replace:
<?php echo $form->fileField($model,'url',array('size'=>60,'maxlength'=>255)); ?>

to
<?php echo $form->fileField($model,'url',array('size'=>60)); ?>

;)
Best wishes,

Ricardo
Ricardo Obregón

YiiFramework en Español - http://yiiframework.co/ - http://yiiframeworkenespanol.org/ - Yii Code Generator for Bootstrap
http://obregon.co/ - https://1server.co/
PHP 5.5+, nginx 1.7, MySQL(MariaDB & PerconaDB), PostgreSQL 9, Yii 2, CanJS
Follow me: @robregonm & @obregonco & @1ServerCo.
0

#3 User is offline   Farhad Arjmand 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 1
  • Joined: 15-August 12

Posted 07 December 2012 - 01:20 PM

tnx. :rolleyes:

Farhad-Arjmand@msn.com
0

#4 User is offline   Kuroz 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 2
  • Joined: 02-April 13

Posted 09 May 2013 - 06:29 AM

If I want to upload another image for another field in the same model, how can I implement it?
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users