file_get_contents

Iam trying toopen a jason file and decode it but o get this error on file_get_content:

file_get_contents(\country.json) [<a href=‘function.file-get-contents’>function.file-get-contents</a>]: failed to open stream: No such file or directory




        $string = file_get_contents(Yii::getPathOfAlias('webroot/assets') . DIRECTORY_SEPARATOR ."country.json");

        $paises=json_decode($string,true);

        print_r($paises);

try webroot.assets

and for testing also replace DIRECTORY_SEPARATOR to "/" if you are in windows system.

Thank you

Actually, PHP should take care of that for you :)

thank you ,it’s working

I an extension I have to manually put that :)

can u tell us about the solution?

tried webroot.assets

Now instead of printing the array i want to populate an activeDropDownList and i achieved that with this code:

controller:


public function getPaises(){

        

        $string = file_get_contents(Yii::getPathOfAlias('webroot.assets') . DIRECTORY_SEPARATOR ."country.json");

        return json_decode($string,true);

        


    }


public function actionUpdate($id) {

        $model = $this->loadModel($id);


        if (isset($_POST['User'])) {

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

            if ($model->save()) {

                Yii::app()->user->setFlash('success', Yii::t('app', 'Data saved'));

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

            }

        }


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

            'mostraPass' => false,

            'model' => $model

        ));

    }




view:




  <div class="row">

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

        <?php echo CHtml::activeDropDownList($model,'country', $this->getPaises()); ?>

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

    </div>



countries are displayed and when i choose one it is saved on database.The only problem is when i load the page the index country are always the same (the first one) and not the country saved.

No PHP shouldn’t bother with this at all. Anything above Windows NT 3.5 should handle / as well as \.

You should reread the documentation concerning the $option-parameter (if I understood your problem ;))

$option is expecting an array with $value=>$text pairs. As we do not know how your json file looks like, this could be your solution.

http://www.yiiframework.com/doc/api/1.1/CHtml#activeDropDownList-detail

Also why did you chose json_decode over CJSON::decode()?

Sorry i forgot to mention the json:

{"AC":"Ascension Island","AD":"Andorra"…

What’s the difference?

Except that / and \ have different meanings on different operating systems. However, as far as PHP functions are concerned, they are usually clever enough to replace / with \ (and vice-versa) as needed. One notable exclusion is realpath() ;)

Well, I miswrote that maybe ;)

I meant to use / everywhere which should be safe in almost every modern OS(atleast in filesystem functions). \ is critical on everything non-windows and even in some windows-actions.

About realpath:

I’ not sure, but afaik win32-api returns path with backslashes and could be outdated a bit in some parts(but I have no clue how php accesses the hosts-filesystem)

Ah, it’s all very grown … Windows knows C:\ as well as \\ but sees / as the start of a parameter. Then again, spaces in path- and filenames are treated in a hackish way. Both, WinDOS and Unix, know \ as a shell escape for e.g. spaces and other stuff.

realpath() in very special in any regard as it not only tries to cut out relative locations (such as ../../) but also tries to verify that the resulting file location actually exists. As such, realpath() will return false if you feed it something like /usr\local/share.

But as far as file_get_contents goes, PHP is smart enough to translate path seperators into the according platform specific character. Ofc, on a framework-level one should spare PHP from any guessing by applying DIRECTORY_SEPARATOR. But I think it’s fine if you don’t do so in your app :)

Anyone knows how to set on the combo the value of the database?

No.After all it does not save on database.Do you know why?

I saw the values in $_POST[‘User’] and the value is there but after that in the $model->attributes dont

I get it.The attribute is not null on databse and i forgot to set required on model rules.Thank you