Hi,
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
print_r($_FILES);
}
else {
echo "Upload Failed!!!";
print_r($_FILES);
}
and this is the function in Yii controller
public function actionUpload(){
$model=new LoginForm;
//var_dump($_FILE);exit;
//$model->attributes=$_POST['SplWish'];
$rand_name = rand(100,9999);
$uploadedFile = CUploadedFile::getInstance($model,'the_file');
if($uploadedFile){
$model->the_file = "{$uploadedFile}";
$model->the_file ="wish_{$rand_name}-{$model->the_file}";
$uploadedFile->saveAs(Yii::app()->basePath.'/../images/'.$model->the_file);
$x = array( 'status' => 1 ,'responsemesssage' =>'Upload Success' );
echo CJSON::encode($x);
Yii::app()->end();
}else{
$x = array( 'status' => 0 ,'responsemesssage' =>'Upload Failled' );
echo CJSON::encode($x);
Yii::app()->end();
}
}
please help me
Ankit_Modi
(Ankit Modi)
September 7, 2013, 4:42am
2
you can check if else condition…
for e.g
if(($_FILES['Coupon']['name']['image']==''))
{
Yii::app()->user->setFlash('error', Yii::t("messages","Please Upload a One of Them Images"));
$this->render('index',array('model'=>$model,));
Yii::app()->end();
}else{
$path = YiiBase::getPathOfAlias('webroot');
$url ='http://'.$_SERVER['HTTP_HOST']. Yii::app()->baseUrl;
$model->image=$_FILES['Coupon']['name']['image'];
$model->image = CUploadedFile::getInstance($model, 'image');
$model->image->saveAs($path.'/upload/coupon/'.$model->image);
$image_path=$url.'/upload/coupon/'.$model->image;
$model->image_url=$image_path;
}
you can check if else condition…
for e.g
if(($_FILES['Coupon']['name']['image']==''))
{
Yii::app()->user->setFlash('error', Yii::t("messages","Please Upload a One of Them Images"));
$this->render('index',array('model'=>$model,));
Yii::app()->end();
}else{
$path = YiiBase::getPathOfAlias('webroot');
$url ='http://'.$_SERVER['HTTP_HOST']. Yii::app()->baseUrl;
$model->image=$_FILES['Coupon']['name']['image'];
$model->image = CUploadedFile::getInstance($model, 'image');
$model->image->saveAs($path.'/upload/coupon/'.$model->image);
$image_path=$url.'/upload/coupon/'.$model->image;
$model->image_url=$image_path;
}
CUploadedFile::getInstance($model, 'image');
it return NULL.
BlkRaven
(Renity1001)
September 8, 2013, 3:13am
4
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?
yugenekr
(Yugenekr)
September 9, 2013, 9:54am
5
If a file isn’t uploaded check you added enctype attribute to your form, e.g.
<?php $form=$this->beginWidget('CActiveForm', array(
'id'=>'config-form',
'enableClientValidation'=>true,
'clientOptions'=>array(
'validateOnSubmit'=>true,
),
'htmlOptions'=>array(
'enctype'=>'multipart/form-data'
)
)); ?>
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.
harilal1234
(Harilal1234)
September 11, 2013, 11:43am
6
If a file isn’t uploaded check you added enctype attribute to your form, e.g.
<?php $form=$this->beginWidget('CActiveForm', array(
'id'=>'config-form',
'enableClientValidation'=>true,
'clientOptions'=>array(
'validateOnSubmit'=>true,
),
'htmlOptions'=>array(
'enctype'=>'multipart/form-data'
)
)); ?>
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