Where did the uploaded file go?

Hi all,

I was testing out how to upload a file as per this example

http://www.yiiframework.com/wiki/2/how-to-upload-a-file-using-a-model/

(BTW it should say CUploadedFile not CUploadFile)

It seems to be doing something but I know not what. Where did the file I “uploaded” go? I’m working on “localhost”.

THe print_r($model) says

"Pic Object ( [image] => CUploadedFile Object ( [_name:private] => diver.jpg [_tempName:private] => C:\PROGRA~1\EASYPH~1.0B1\\tmp\php563.tmp [_type:private] => image/jpeg [_size:private] => 23136 [_error:private] => 0 [_e:private] => [_m:private] => ) [_errors:private] => Array ( ) [_validators:private] => [_scenario:private] => [_e:private] => [_m:private] => ) "

I think it got the filename right, but that’s all I could tell.

My code :




//in the Models/Pic.php

class Pic extends CFormModel

{

public $image;


public function rules()

	{

	return array('image', 'file', 'types'=>'jpg');

	

	

	}


}




//in the Controllers/PicController.php

class PicController extends CController

{

    public function actionCreate()

    {

        $model=new Pic;

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

        {

            $model->image=$_POST['Pic'];

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

            //if($model->Save())   

            //CFormModel does not have a Save method? but has a SaveAs?

                $model->image->saveAs('forestry1/');

                // redirect to success page

            echo "success?";

			print_r($model);

			

        }

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

    }

}



and the View is just:


<?php echo CHtml::form('','post',array('enctype'=>'multipart/form-data')); ?>

...

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

...

<?php echo CHtml::submitButton('Create', array('submit'=> 'Create')) ; ?>

<?php echo CHtml::endForm(); ?>

The image file "diver.jpg" does not appear in the folder? What happened? What did the code do?

Hello,

I read the comments and noticed that the first one said:




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

// As others have noted: $model->image at the end of the path is required,    // and is the filename of your image




$model->image->saveAs('forestry1/');



Is trying to save an image as a folder ??

try




$model->image->saveAs('forestry1/'.$model->image->getName());

 

You can also define your path as a named constant




define("PATH_UPLOAD_IMAGE",     "/upload/");

define("ABSOLUTE_PATH_UPLOAD_IMAGE", realpath(dirname('../'.PATH_UPLOAD_IMAGE)."/".PATH_UPLOAD_IMAGE));

// it's an example, define ABSOLUTE_PATH_UPLOAD_IMAGE as your needs



And use it like Gustavo and beninblack:




$model->image->saveAs(ABSOLUTE_PATH_UPLOAD_IMAGE."/{$model->image->getName()}");

 

Thanks guys, it works now!

Somebody should go fix the errors in the tutorial though! Yii is hard enough as it is. :)

Can you give more details… link to the doc… and what is written wrongly…