Quantcast
Channel: Yii Framework Forum
Viewing all articles
Browse latest Browse all 18717

File Upload Action Update Problem

$
0
0
Here is my dilemma, I can upload files, delete files, but update is not working correct. Of course if I change the file then I can update the whole record but If I want to just update the description field then I get of course: Filename cannot be blank. This is supposed to be supplied by a hidden field as a part of activeFileField, but why not just simply return the path back to this field on update. Anyway how do I go about fixing this?

My Form
<?php
/* @var $this UserfilesController */
/* @var $model Userfiles */
/* @var $form CActiveForm */
?>

<div class="form">

<?php $form=$this->beginWidget('CActiveForm', array(
	'id'=>'userfiles-form',
	'enableAjaxValidation'=>false,
	'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,'filename'); ?>
		<?php echo CHtml::activeFileField($model, 'filename'); ?>
        <?php //echo $form->textField($model,'filename',array('size'=>60,'maxlength'=>77)); ?>
		<?php echo $form->error($model,'filename'); ?>
	</div>
    <div class="row">
     <?php echo $form->labelEx($model,'description'); ?>
     <?php echo $form->textArea($model,'description',array('rows'=>5,'cols'=>50)); ?>
     <?php echo $form->error($model,'description'); ?>
    
    </div>

	<div class="row">
		<?php echo $form->labelEx($model,'user_id'); ?>
		<?php echo $form->dropDownList($model, 'user_id', CHtml::listData(User::model()->findAll(), 'id', 'username'),array('prompt' => 'Select a User')); ?>
		<?php echo $form->error($model,'user_id'); ?>
	</div>

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

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

</div>&lt!-- form -->



update function
public function actionUpdate($id)
	{
		$model=$this->loadModel($id);
                $old=$this->loadModel($id);
		// Uncomment the following line if AJAX validation is needed
		// $this->performAjaxValidation($model); 

		if(isset($_POST['Userfiles']))
		{
			$model->attributes=$_POST['Userfiles'];
		if($model->save())
				
		{
		$model->filename=CUploadedFile::getInstance($model,'filename');
		
		
              if (!empty($_FILES['Userfiles']['filename']['name'])) {
                if ($old->filename != "") {
                $file= 'images/$old->filename->name';
                                        unlink($file);
                }          
                        } else {
								if(!is_null($model->filename)) {
                                $model->filename->name;
								}
                        }       
            if($model->save())
            {
				
				if(!is_null($model->filename)) {
            	$file= 'images/'.$model->filename->name;
				
            	$model->filename->saveAs($file);
                //model->filename->saveAs(Yii::app()->baseUrl.'/images');
                // redirect to success page
            	}
				$this->redirect(array('view','id'=>$model->id));
				
			}
            	
				
			
		}
		}

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

Viewing all articles
Browse latest Browse all 18717

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>