0

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

11
  • does the file really exists? Commented Dec 17, 2014 at 14:16
  • /dev? Sure? This is the *nix devices subsystem. It would be very weird to have JPEGs stored there. Commented Dec 17, 2014 at 14:16
  • @YeahBoy yes it does, like i said, when i return $img on line 3, the image shows. Commented Dec 17, 2014 at 14:17
  • Where do you use that $img string? In an <img> tag? Commented Dec 17, 2014 at 14:18
  • Hmmm... What @BartFriederichs means is that this would be a very bad choice for a folder to store the image... Like storing your regular documents in `C:\Windows\System32` would be a bad choice on Windows machines Commented Dec 17, 2014 at 14:18

2 Answers 2

1

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 } 
Sign up to request clarification or add additional context in comments.

Comments

0

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

2 Comments

still don't work url your code generates is /Applications/XAMPP/xamppfiles/htdocs/dev/frontend/www/images/brand-pages/make/lol-976x365.jpg
once again can you check if the image is available pls, atleast with the url directly

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.