Alternate File Uploading Method For Yii


Im working on a C# - PHP Synchronysation application. When C# try to upload an image the core php file upload can handle the file and it works fine.

But when I try with Yii "CUploadedFile" it didnot return file name and got error.

Here is my code in core php and yii

$uploaddir = 'upload/'; // Relative Upload Location of data file

if (is_uploaded_file($_FILES['file']['tmp_name'])) {

$uploadfile = $uploaddir . basename($_FILES['file']['name']);

echo "File ". $_FILES['file']['name'] ." uploaded successfully.";

if (move_uploaded_file($_FILES['file']['tmp_name'], $uploadfile)) {

echo "File is valid, and was successfully moved.";





else {

echo "Upload Failed!!!";



and this is the function in Yii controller

public function actionUpload(){

		$model=new LoginForm;



			$rand_name = rand(100,9999);

			$uploadedFile = CUploadedFile::getInstance($model,'the_file');


			$model->the_file = "{$uploadedFile}";

			$model->the_file ="wish_{$rand_name}-{$model->the_file}";



				$x = array( 'status' => 1 ,'responsemesssage' =>'Upload Success' );

				echo CJSON::encode($x);




			$x = array( 'status' => 0 ,'responsemesssage' =>'Upload Failled' );

				echo CJSON::encode($x);				







please help me

you can check if else condition…

for e.g



					Yii::app()->user->setFlash('error', Yii::t("messages","Please Upload a One of Them Images"));




					$path	= 	YiiBase::getPathOfAlias('webroot');

					$url ='http://'.$_SERVER['HTTP_HOST']. Yii::app()->baseUrl;


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





CUploadedFile::getInstance($model, 'image');

it return NULL.

The API documentation says that when using CUploadedFile::getInstance(), the file should be uploaded using CHtml::activeFileField. Could that be the problem?

Have you tried using CUploadedFile::getInstanceByName() instead?

If a file isn’t uploaded check you added enctype attribute to your form, e.g.

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









)); ?>

If you still don’t get the file pls send related model rules, related parts of view file and current state of the handling action/method.

Im trying to upload file from Windows application (C#). when I use "$_FILE" the file will upload but the file is not uploading when I use Yii "GetInstand". How can I upload my file using Yii