Image upload problem

I’ve looked through the posts and haven’t found a solution or even someone else with this same problem. That means to me that it’s probably something really simple that I’m overlooking :P

Whenever I try to upload an image, I keep getting the following error:


move_uploaded_file(/BuilderApp/public_html/images/BirchwoodIFP.gif) [<a href='function.move-uploaded-file'>function.move-uploaded-file</a>]: failed to open stream: No such file or directory

But, I know that directory exists because that’s where all my images are located and they appear in the application.

Here is my controller:


	public function actionCreate()

	{

		$model=new Floorplan;


		// Uncomment the following line if AJAX validation is needed

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


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

		{

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

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

			$model->floorplanURL = '/'.$model->floorplanFile;

			if($model->save())

			{

				$model->floorplanFile->saveAs(Yii::getPathOfAlias('images').'/'.$model->floorplanFile);

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

			}

		}


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

			'model'=>$model,

		));

	}



My _form:


<div class="form">


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

	'id'=>'floorplan-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,'name'); ?>

		<?php echo $form->textField($model,'name',array('size'=>20,'maxlength'=>20)); ?>

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

	</div>


	<div class="row">

		<?php echo $form->labelEx($model,'planID'); ?>

		<?php echo $form->textField($model,'planID',array('size'=>10,'maxlength'=>10)); ?>

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

	</div>


	<div class="row">

		<?php echo $form->labelEx($model,'floorplanURL'); ?>

		<?php echo $form->fileField($model,'floorplanFile'); ?>

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

	</div>


	<div class="row">

		<?php echo $form->labelEx($model,'deleted'); ?>

		<?php echo $form->textField($model,'deleted',array('size'=>1,'maxlength'=>1)); ?>

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

	</div>


	<div class="row buttons">

		<?php echo CHtml::submitButton($model->isNewRecord ? 'Create' : 'Save'); ?>

	</div>


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


</div><!-- form -->

Any help would be greatly appreciated.

Thanks in advance.

Folder permissions aren’t set properly.

Do a "chmod -R * 755" for that folder.

Do you know that you are trying to save the CUploadedFile into your model?

What exactly do you want to save in your $model->floorplanFile??

What you did is getting a reference to CUploadedFile, afterwards you save the attributes to the DB and then you save the file. IMHO I think this is all wrong, or I guess I never done it this way.

In order to help you, please, what are you saving on floorplanFile? The name of the file, the file contents itself as binary?

If you want to store images into DB this post may help you: http://www.yiiframework.com/forum/index.php?/topic/8749-images/page__p__43737__hl__+aving+image+DB#entry43737

But if you wish to get just the file name so to reference aftewards:

a) configure the floorplanFile attribute as safe and no required in your rules

b ) write a fieldField in your view like this:




<?php echo CHtml::activeLabel($model,'floorplanFile'); ?> 

<?php echo CHtml::fileField('photo',''); ?>



c) on your controller




$picture_file = CUploadedFile::getInstanceByName('photo');

// has the user uploaded a new file?

if($picture_file){

	

	$picture_name = $picture_file->name;

	$picture_file->SaveAs('yourpathtosavefiles/' . $picture_name);	

}

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

$model->picture = $picture_name;

$model->save();

...



Of course the above code is shown as an example, it is TOO SIMPLE

Check on the Wiki how to use PHPThumb to make thumbnails. Forum is full of good posts from colleague programmers about how to handle image submissions.

Best

Thanks for your replies…

@ Dheeraj - I checked my permissions and they are set correctly

@ Antonio - I followed the example here: http://www.yiiframework.com/wiki/2/. The only thing I did differently was add


$model->floorplanURL = '/'.$model->floorplanFile;

FloorplanURL is the column in the database that holds the file name.

Do you see something that I did differently than the example?

Excuse my first posts as I didnt see your Model Configuration, if you have set your rules properly and also had a public property named $model->floorplanFile then your error is this one:




// you need to include the name <img src='http://www.yiiframework.com/forum/public/style_emoticons/default/smile.gif' class='bbc_emoticon' alt=':)' /> at the end

$model->floorplanFile->saveAs(Yii::getPathOfAlias('images').'/'.$model->floorplanFile->name);



I’ve just tried to use this Image extension, http://www.yiiframework.com/extension/image with a different model and still got the same type of error…


CException

Description


image file not found

Source File


C:\xampp\htdocs\BuilderApp\public_html\protected\extensions\image\Image.php(78)


00066:     public function __construct($image, $config = NULL)

00067:     {

00068:         static $check;

00069: 

00070:         // Make the check exactly once

00071:         ($check === NULL) and $check = function_exists('getimagesize');

00072: 

00073:         if ($check === FALSE)

00074:             throw new CException('image getimagesize missing');

00075: 

00076:         // Check to make sure the image exists

00077:         if ( ! is_file($image))

00078: throw new CException('image file not found');

00079: 

00080:         // Disable error reporting, to prevent PHP warnings

00081:         $ER = error_reporting(0);

00082: 

00083:         // Fetch the image size and mime type

00084:         $image_info = getimagesize($image);

00085: 

00086:         // Turn on error reporting again

00087:         error_reporting($ER);

00088: 

00089:         // Make sure that the image is readable and valid

00090:         if ( ! is_array($image_info) OR count($image_info) < 3)


Stack Trace


#0 C:\xampp\htdocs\BuilderApp\public_html\protected\views\communityPlan\view.php(47): Image->__construct('/BuilderApp/pub...')

#1 C:\xampp\htdocs\YiiTutorial\framework\web\CBaseController.php(119): require('C:\xampp\htdocs...')

#2 C:\xampp\htdocs\YiiTutorial\framework\web\CBaseController.php(88): CBaseController->renderInternal('C:\xampp\htdocs...', Array, true)

#3 C:\xampp\htdocs\YiiTutorial\framework\web\CController.php(798): CBaseController->renderFile('C:\xampp\htdocs...', Array, true)

#4 C:\xampp\htdocs\YiiTutorial\framework\web\CController.php(739): CController->renderPartial('view', Array, true)

#5 C:\xampp\htdocs\BuilderApp\public_html\protected\controllers\CommunityPlanController.php(61): CController->render('view', Array)

#6 [internal function]: CommunityPlanController->actionView('1')

#7 C:\xampp\htdocs\YiiTutorial\framework\web\actions\CInlineAction.php(47): ReflectionMethod->invokeArgs(Object(CommunityPlanController), Array)

#8 C:\xampp\htdocs\YiiTutorial\framework\web\CController.php(300): CInlineAction->run()

#9 C:\xampp\htdocs\YiiTutorial\framework\web\filters\CFilterChain.php(133): CController->runAction(Object(CInlineAction))

#10 C:\xampp\htdocs\YiiTutorial\framework\web\filters\CFilter.php(41): CFilterChain->run()

#11 C:\xampp\htdocs\YiiTutorial\framework\web\CController.php(1049): CFilter->filter(Object(CFilterChain))

#12 C:\xampp\htdocs\YiiTutorial\framework\web\filters\CInlineFilter.php(59): CController->filterAccessControl(Object(CFilterChain))

#13 C:\xampp\htdocs\YiiTutorial\framework\web\filters\CFilterChain.php(130): CInlineFilter->filter(Object(CFilterChain))

#14 C:\xampp\htdocs\YiiTutorial\framework\web\CController.php(283): CFilterChain->run()

#15 C:\xampp\htdocs\YiiTutorial\framework\web\CController.php(257): CController->runActionWithFilters(Object(CInlineAction), Array)

#16 C:\xampp\htdocs\YiiTutorial\framework\web\CWebApplication.php(324): CController->run('view')

#17 C:\xampp\htdocs\YiiTutorial\framework\web\CWebApplication.php(121): CWebApplication->runController('communityPlan/v...')

#18 C:\xampp\htdocs\YiiTutorial\framework\base\CApplication.php(135): CWebApplication->processRequest()

#19 C:\xampp\htdocs\BuilderApp\public_html\index.php(13): CApplication->run()

#20 {main}



Here is a piece of my view file in which I use the extension just to load a local image file from the images folder.


	Yii::import('application.extensions.image.Image');

	$img = new Image(Yii::getPathOfAlias('images').$thisElevation->elevationURLBig);

	$img->render();



Even if I hard code the path, I still get the error.

The image renders just fine if I comment out the above code and replace it with this…




	echo CHtml::image(Yii::getPathOfAlias('images').$thisElevation->elevationURLBig);



So, I know the image exists in that location.

I still get the same error. Plus, if you look at the error in my first post, you will see that the file name is there at the end of the path. So, I’m not sure ‘->name’ is necessary.

Could you do something you may thing is silly?

Could you do

Yii::getPathOfAlias(‘webroot.images’) instead?

** I know you said that even hardcoded you still get an error also use DIRECTORY_SEPARATOR

or

Could you save the file before the $model->save(),

** Maybe the attributes are changing on save (didnt look into the code flow yet)

Lets see see what happens

I have just seen you have windows, then why the path is given /BuildApp/ instead of a windows fullpath? C:\… have you checked that?

That did the trick! I knew it had to be something simple that I was overlooking.

Thanks for all your help! I really appreciate it.

Very happy I could help… It was getting on my nerves already :)

Congrats man