user2636556
user2636556

Reputation: 1915

file_exists() not working

I'm using Yii 1.1.15 and am trying to use a simple file_exists() php function. But i cant get it to work.

when i just return the image url, the image shows. but when i use file_exists() it returns $img2

function in my model

 public function brandPageImage($make = false) {
        $urlstr = Yii::app()->request->baseUrl.'/images/brand-pages/make/';
        $img = $urlstr.trim(strtolower($make)).'-976x365.jpg';
        $img2 = $urlstr.'default-976x365.jpg';

        clearstatcache();

        if (file_exists($img)) return $img;

        return $img2;
 }

if i return $img directly it works!

 public function brandPageImage($make = false) {
            $urlstr = Yii::app()->request->baseUrl.'/images/brand-pages/make/';
            return $img = $urlstr.trim(strtolower($make)).'-976x365.jpg';
     }

this is the url it generates

/dev/frontend/www/images/brand-pages/make/lol-976x365.jpg

can someone help me, don't see what i'm doing wrong here. I'm running XAMMP on OSX Thanks

Upvotes: 0

Views: 238

Answers (2)

A l w a y s S u n n y
A l w a y s S u n n y

Reputation: 38502

you should use getPathOfAlias instead of baseUrl,Because Yii::app()->request->baseUrl returns base URL, not document root path.

  if (file_exists(YiiBase::getPathOfAlias('webroot').'images/brand-pages/make/‌​
                 lol-976x365.jpg')) 
 { 
      // do what you want to do
 }

Upvotes: 1

Jenno Richi Benat
Jenno Richi Benat

Reputation: 671

Try with since it need the path and not the url where the image resides

 public function brandPageImage($make = false) {
        $urlstr = dirname(Yii::app()->request->scriptFile).'/images/brand-pages/make/';
        $img = $urlstr.trim(strtolower($make)).'-976x365.jpg';
        $img2 = $urlstr.'default-976x365.jpg';

        clearstatcache();

        if (file_exists($img)) return $img;

        return $img2;
 }

Upvotes: 0

Related Questions