Paths like './assets/images/test.jpg' failing on local

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
mattpointblank
Forum Contributor
Posts: 304
Joined: Tue Dec 23, 2008 6:29 am

Paths like './assets/images/test.jpg' failing on local

Post by mattpointblank »

Hi there all.

I'm integrating phpThumb (a thumbnailing script) with CodeIgniter framework. Both scripts are quite fussy about paths/URLs (in the sense that they're not structured 'physically' through CI, but re-routed).

Anyway, I've been doing some development on my work machine using functions like:

Code: Select all

 
if(file_exists('./assets/images/test.jpg')) { echo 'exists'; } else { echo 'not found'; }
 
This works at my work machine, which has the same folder structure (and yes, the file does exist!)

If I replace the path with the absolute one (eg '../../../assets' etc) it works, but this isn't practical and given CodeIgniter's dynamic URLs, I can't always predict how many levels deep the ../s need to be.

Can anyone think of some php configuration issue that could be causing this? It's driving me mad trying to get these root paths working!

Matt
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Paths like './assets/images/test.jpg' failing on local

Post by requinix »

Paths like "./assets/images/test.jpg" are relative to the current working directory. That's not always the same as the directory the current script is in.
mattpointblank wrote:If I replace the path with the absolute one (eg '../../../assets' etc) it works
That's not absolute - it's still relative.

An absolute path looks like

Code: Select all

$_SERVER["DOCUMENT_ROOT"] . "/assets/images/test.jpg"
dirname(__FILE__) . "/assets/images/test.jpg"
The former is when you know that /assets is in the document root, the latter is when /assets is in the same directory as the script.
mattpointblank
Forum Contributor
Posts: 304
Joined: Tue Dec 23, 2008 6:29 am

Re: Paths like './assets/images/test.jpg' failing on local

Post by mattpointblank »

Ah, thank you. I tried echoing out getcwd() and it was the same root directory that my assets folder is in, but because of CodeIgniter's structure, the URL to my page is something like mysite.com/articles/view/123, which complicates paths and links. I'll try using one of the above methods and see if that makes a difference.
Post Reply