Hey everyone,
I am using a BLOB datatype in my database to store uploaded files. That is working well they are uploading and storing properly, and they can be reconstructed.
The problem I am having is in validation. I require the files to ONLY be in .ZIP format. So I applied the validation rule:
array('uploadedFile', 'file', 'types'=>'zip' ,'allowEmpty'=>false,'wrongType'=>'File must be in .ZIP format!'),
It is only partially working
If the file is not in .zip format it will not upload it or store it in the database, but it wont display an error message.
Here is the code pertaining to the uploader:
Controller:
public function actionUpload()
{
$model=new TsPlusFiles;
// Uncomment the following line if AJAX validation is needed
// $this->performAjaxValidation($model);
if(isset($_POST['TsPlusFiles']))
{
$model->attributes=$_POST['TsPlusFiles'];
if($model->save())
$this->redirect(array('upload'));
}
$this->render('upload',array(
'model'=>$model,
));
}
Model:
public function rules()
{
// NOTE: you should only define rules for those attributes that
// will receive user inputs.
return array(
array('uploadedFile', 'file', 'types'=>'zip' ,'allowEmpty'=>false,'wrongType'=>'File must be in .ZIP format!'),
);
}
/**
* Saves the name, size, type and data of the uploaded file
*/
public function beforeSave() {
if($file=CUploadedFile::getInstance($this,'uploadedFile')) {
$this->file_name=$file->name;
$this->file_type=$file->type;
$this->file_size=$file->size;
$this->file_content=file_get_contents($file->tempName);
}
return parent::beforeSave();
}
View:
<div class="form">
<?php $form=$this->beginWidget('CActiveForm', array(
'id'=>'tsPlusFiles-form',
'htmlOptions'=>array('enctype'=>'multipart/form-data'),
'enableClientValidation'=>true,
'clientOptions'=>array(
'validateOnSubmit'=>true,
),
)); ?>
<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,'uploadedFile'); ?>
<?php echo $form->fileField($model,'uploadedFile'); ?>
<?php echo $form->error($model,'uploadedFile'); ?>
</div>
<div class="row buttons">
<?php echo CHtml::submitButton('Submit'); ?>
</div>
<?php $this->endWidget(); ?>
</div><!-- form -->
I have tried it with and without client side validation, and I still get the same result, it wont upload the file, but it also won’t display an error message.