Kristian Rafteseth
Kristian Rafteseth

Reputation: 2032

PHP pathing trouble

I have a file, functions.php. Within the file is some functions that point at specific locations, like thisone:

function returnFolders() {
    $folders = array_filter(glob("images/*"), 'is_dir');
    return $folders;
}

This function returns the directories within the images/ folder, where the functions.php file is located.

The problem is that if i include this function.php from a script in another folder than function.php resides in, this function will not give the right result.

How can this be solved, so it will always return the folders where functions.php is located?

Paths looks like this:

common/functions.php
common/images/1
common/images/2
common/crons/im_including_functions.php
dir1/im_also_including_functions.php
d3/me_too.php

Upvotes: 1

Views: 62

Answers (2)

Piya Pakdeechaturun
Piya Pakdeechaturun

Reputation: 141

it's about relative path and absolute path when you use relative path it's start from the file that run your code

so first you should understand these two type of path

http://webdesign.about.com/od/beginningtutorials/a/aa040502a.htm

and later on you can find the best way for your solution to fix this

for a little help about php function about the path http://php.net/manual/en/function.realpath.php

hope this help ;)

Upvotes: 0

crush
crush

Reputation: 17013

Check out magic constants.

You can use __DIR__ to get the current script directory, for example.

Without knowing more about your directory structure, I can't really be more specific.


On second thought, it looks to me like a better solution for your case would be to use an absolute path to the images folder. That way, no matter where this function is run, it would look for images in the same place.

Upvotes: 1

Related Questions