Question

I am working on Yii project. It's working perfectly on my localhost. As I uploaded project on server, I am getting error on server on product csv upload page and it is giving me error as

ImportcsvController cannot find the requested view "import".

I have Importcsv Controller file, Importcsv folder( containing import.php and importcsv.php ) inside view folder and Importcsv model file. Everything is working fine on localhost but I don't understand why it's giving me error on server. All files are uploaded perfectly.

My Controller is

<?php
class ImportcsvController extends Controller
{
/**
 * @var string the default layout for the views. Defaults to '//layouts/column2', meaning
 * using two-column layout. See 'protected/views/layouts/column2.php'.
 */
public $layout='//layouts/main';
public $defaultAction='import';

/**
 * @return array action filters
 */
public function filters()
{
    return array(
        'accessControl', // perform access control for CRUD operations
        'postOnly + delete', // we only allow deletion via POST request
    );
}

/**
 * Specifies the access control rules.
 * This method is used by the 'accessControl' filter.
 * @return array access control rules
 */
public function accessRules()
{
    return array(
        array('allow',  // allow all users to perform 'index' and 'view' actions
            'actions'=>array('view','import'),
            'users'=>array('*'),
        ),

    );
}

/**
 * Displays a particular model.
 * @param integer $id the ID of the model to be displayed
 */
public function actionView($id)
{
    $this->render('view',array(
        'model'=>$this->loadModel($id),
    ));
}



    public function actionImport() {

        $model = new Importcsv;
        $file = CUploadedFile::getInstance($model,'csv_file');
        if(isset($_POST['Importcsv']))
        {
            $model->attributes=$_POST['Importcsv'];
            if(!empty($_FILES['Importcsv']['tmp_name']['csv_file']))
            {
                $file = CUploadedFile::getInstance($model,'csv_file');


                $fp = fopen($file->tempName, 'r');
                if($fp)
                {
                     $line = fgetcsv($fp, 1000, ",");

                    $first_time = true;


                                            //begin transaction
                                            $transaction=Yii::app()->db->beginTransaction();

                                        try {


                                            //truncate table before importing csv
                                            Yii::app()->db->createCommand("truncate table products")->execute();



                                            //add csv ecords

                                                        do {
                                                                if ($first_time == true) {
                                                                        $first_time = false;
                                                                        continue;
                                                                }
                                                                $model = new Products;

                                                                $model->product_name  = $line[1];
                                                                $model->product_code  = $line[2];
                                                                $model->price  = $line[3];
                                                                $model->mrp  = $line[4];
                                                                $model->quantity  = $line[5];
                                                                if($line[6]==''){ 
                                                                        $model->image  = 'noimage.jpg';
                                                                }
                                                                else {
                                                                        $model->image  = $line[6];
                                                                }
                                                                $model->status  = $line[7];

                                                                $model->created_at  = date('Y-m-d h:i:s');

                                                                $model->save();
                                                        }while( ($line = fgetcsv($fp, 1000, ",")) != FALSE);

                                            $transaction->commit();
                                             } catch (Exception $e) {
                                            $transaction->rollback();
                                             }


                                    }

            }

            $this->redirect(array("Products/index"));

        }


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

    }



/**
 * Performs the AJAX validation.
 * @param Company $model the model to be validated
 */
protected function performAjaxValidation($model)
{
    if(isset($_POST['ajax']) && $_POST['ajax']==='importcsv-form')
    {
        echo CActiveForm::validate($model);
        Yii::app()->end();
    }
}
}

?>

My view is (import.php)

<?php
 // var_dump($model);
 $this->renderPartial('importcsv', array('model'=>$model)); 
?>

importcsv.php is

<div class="form">





<?php $form=$this->beginWidget('bootstrap.widgets.TbActiveForm', array(

'id'=>'importcsv-form',

'enableAjaxValidation'=>true,

'type' => 'horizontal',

'htmlOptions' => array('enctype'=>'multipart/form-data', 'class'=>'col-sm-offset-   2'),

));



?>



<fieldset>

    <legend>

        <p class="note">Fields with <span class="required">*</span> are required.</p>

    </legend>





    <div class="control-group">     

        <div class="span4">

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

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

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

        </div>

    </div>



    <div class="row buttons  col-sm-offset-2">

        <?php echo CHtml::submitButton('Upload CSV',array("class"=>"btn btn-primary")); ?> <?php echo $form->errorSummary($model); ?>

    </div>

</fieldset>



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

My Model is(importcsv.php)

<?php

class Importcsv extends CFormModel
{
public $csv_file;
/**
 * @return array validation rules for model attributes.
 */
public function rules() { 
        return array( array('csv_file', 'file', 'types' => 'csv' ,
            'maxSize'=>5242880, 
            'allowEmpty' => true, 
            'wrongType'=>'Only csv allowed.', 
            'tooLarge'=>'File too large! 5MB is the limit'
            )
            );  


} 

public function attributeLabels() { 
        return array('csv_file'=>'Import CSV',); 
}

}

?>

I don't know where I am going wrong...Please help me and Thanks in advance....

Was it helpful?

Solution

the first letter of the directory created in protected/views cannot start with a capital letter (on Linux systems anyway).

If your hosting service is Linux then please checkout the folder name, this may help you, All the Best

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top