File upload help please

I’m having problems getting a file uploaded using any method. Currently I am trying to upload files using a model and storing them in an OS directory.

Starting at the end, I get this error message.

move_uploaded_file(/hawkwebapp/assets/hawk/images/31-photo.jpg) [<a href=‘function.move-uploaded-file’>function.move-uploaded-file</a>]: failed to open stream: No such file or directory

Please notice that the move_uploaded_file only has one parameter. Even though CUploadedFile.php is using this line of code which contains 2 parameters:

return move_uploaded_file($this->_tempName,$file);

I think part of the message "No such file or directory" refers to the empty parameter? The location of the temp file? If I manually place the correctly named file in the destination directory, it displays just fine.

I have read “How to upload a file using a model” and to the best of my knowledge I have everything entered as suggested. Here’s how I have it set up:

In the model:




public $imagefile;


public function rules()

	{

            return array(

              //more rules

              array('imagefile', 'file', 'types'=>'jpg, gif, png'),

              //more rules

          );

}



In the controller:





public function actionCreate()

	{

		$model=new Image;


		// Uncomment the following line if AJAX validation is needed

		// $this->performAjaxValidation($model);


		if(isset($_POST['Image']))

		{

			$model->attributes=$_POST['Image'];

			$image_file=CUploadedFile::getInstance($model, 'imagefile');

			if(!is_object($image_file))

			{

				$this->redirect(array('update', 'id'=>$model->id));

			}

			if($model->save())

				$internalPath=$model->internal_path;

				

				$image_file->saveAs(Yii::app()->baseUrl.$internalPath);

				$this->redirect(array('view','id'=>$model->id));

		}


		$this->render('create',array(

			'model'=>$model,

		));

	}




In the form:





<div class="row">

        

        <?php echo CHtml::activeFileField($model, 'imagefile'); ?>

        <?php echo $form->error($model,'imagefile'); ?>

        

	</div>




My development environment is running in MAMP on my local Mac. All relevant directories have read/write access for everyone.

I hope someone can help. This should be such a simple thing to accomplish. I’ve tried it with the model upload like here. I’ve tried a database blob approach. I’ve looked at the upload extension. I can’t get anything to work. I’ve been working on this so long my mind is mush.

All help is appreciated.

Alex

I forgot to say that I did add the multipart designation to the form:





<?php $form=$this->beginWidget('CActiveForm', array(

	'id'=>'image-form',

	'enableAjaxValidation'=>false,

	'htmlOptions'=>array(

		'enctype'=>'multipart/form-data',

		),

)); ?>




Hi Alex,

"failed to open stream: No such file or directory" meens the path returned by Yii::app()->baseUrl.$internalPath not found

or have not the right to access to it, please can you check Yii::app()->baseUrl.$internalPath.

Good luck.

Hi alex,

i think the saveAs path was wrong, you try to create a folder inside protected, example images. Then change this code

change from




$image_file=CUploadedFile::getInstance($model, 'imagefile');



to below code




$model->image=CUploadedFile::getInstance($model,'imagefile');



change from




$image_file->saveAs(Yii::app()->baseUrl.$internalPath);



to below code ( assume your path is inside protected/images/




$image_file->saveAs(Yii::app()->baseUrl.'images/'.$model->image;



below this link might help you:

http://www.yiiframework.com/wiki/2/

let me know if this amend was working :)

Hi, Alex

Today, i have a problem like yours, i found the solution i tried by use the file system path in saveAs method. I think move_upload_file interface in file system level , so i use this statement

$model->image->saveAs(Yii::app()->basePath./images/… )

it will store ur image file in /protected/images/ folder

Thank u

Tum