简体   繁体   中英

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

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
 }

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;
 }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM