I am not sure that this will fix your problem but I believe you should also be passing the filename to the saveAs() function, not just the directory to save it in.
I use code like this to allow admins to upload files for my users to access and it works as expected.
Also, try doing a var_dump($model->doc_comment) before the saveAs() call to make sure that $model->doc_comment is indeed being populated by CUploadedFile::getInstance()
I tried using var_dump($model->doc_comment) and $model->doc_comment->saveAs("C:/xampp/htdocs/new/images/" . $model->doc_comment); then I got null followed by fatalerror: Call to a member function saveAs() on a non-object.
var_dump given me null.
The main reason is no file is been getting selected.
I made the field being required in model then it gave error as it cannot be blank.
There are many ways to solve this. But it depends on your requirements. Some options:
Files are uploaded to a public folder in www: Then you only have to create a link with the filename from DB
Files are uploaded to a private directory: Then create a controller action for download. There you can check user permission and use readfile() to send the file to the client through PHP.
I want your help in another small problem regarding uploading files.
I have a form where there is a possibility to upload as many as 14 documents at a time. So I used batch update. The problem I am facing there is an error which says saveas calling non object( I got to know that error occurs if documents are not being read). When I tried with the single document(different code for single) then it worked fine.
public function actionUpdate($id)
{
$model=$this->loadModel($id);
$img_flyer = CUploadedFile::getInstance($model,'image');
if ((is_object($img_flyer) && get_class($img_flyer)==='CUploadedFile'))
$model->image = $img_flyer;
// Uncomment the following line if AJAX validation is needed
// $this->performAjaxValidation($model);
if(isset($_POST['Job']))
{
$model->attributes=$_POST['Job'];
foreach ($_POST['Job'] as $key=>$value)
$model->$key=$value;
if($model->validate()){
$model->save(false);
$contractor = $model->contractor;
$contractor->contact_id = Main::saveContact($model);
$contractor->address_id = Main::saveAddress($model);
if (is_object($img_flyer))
{
print_r($img_flyer);
$id = $contractor->image_id;
$img_flyer="img_contractor_".$id.".".findFileExt($img_flyer);
//$img_flyer = strtolower($img_flyer);
// $fileDir = dirname(Yii::app()->basePath).'/images/contractor/'.$img_flyer;
$model->image->saveAs("D:/xampp/htdocs/contractorgig/images/contractor/orginal/" .$img_flyer);
$width = array('1'=>'48','2'=>'240');
$height = array('1'=>'60','2'=>'300');
$fileDir1 = dirname(Yii::app()->basePath).'/images/contractor/thumbs/big/'.$img_flyer;
$fileDir2 = dirname(Yii::app()->basePath).'/images/contractor/thumbs/small/'.$img_flyer;
for($i=1;$i<=2;$i++):
@copy($fileDir,$fileDir.$i);
$image = Yii::app()->image->load($fileDir.$i);
$image->resize($width[$i],$height[$i],IMAGE::AUTO);
$image->save();
endfor;
$img = Image::model()->findByPk($id);
$img->name = $img_flyer;
$img->save(false);
}
my file upload form file with is
<div class="cf mt-20">
<label for="company_logo" class="col-l tg-label bold">Logo</label>
<?php echo CHtml::activeFileField($model,'image',array('size'=>40,'maxlength'=>255,'class'=>"ml-45")); ?>
<p class="gra mt-8 clear ml-75">
Optional - Your company logo will appear at the top of your listing.
190px wide is optimal. We'll resize automatically if it's wider than
190px.<br />The following image file formats are supported only:
JPEG, GIF, PNG
</p>
</div>
but I can’t get it saved the error with debug the file is
CUploadedFile Object ( [_name:CUploadedFile:private] => ico1.png [_tempName:CUploadedFile:private] => D:\xampp\tmp\php82A8.tmp [_type:CUploadedFile:private] => image/png [_size:CUploadedFile:private] => 5410 [_error:CUploadedFile:private] => 0 [_e:CComponent:private] => [_m:CComponent:private] => )
Fatal error: Call to a member function saveAs() on a non-object in D:\xampp\htdocs\contractorgig\protected\controllers\JobController.php on line 167
Please help me out in this as soon as possible. Thank you