re: php relative paths to files using includes

i have 2 files:

index.php

and

subdirectory/index.php

both are using the same include file with relative links to say images/image.gif

can i set up some sort of configuration file in my root directory that somehow defines the a path so i don't have to make "images/image.gif" absolute in (i.e. - "http://www.mysite.com/images/image.gif"??

if so please explain thoroughly - i've never done this be for (please be gentle LOL)
phillystyle123Asked:
Who is Participating?
 
solutionDriverCommented:

You are using a linux/unix based OS, and I'm sure there is the trailing slash missing in the absolute path:

$image_dir = "/usr/home/healthpresence/orthoneuro/images/";

Maybe this was already the only problem.

I assume that the config.php file is in the dir /usr/home/healthpresence/orthoneuro/includes/.

> am i supposed to add that variable to each file path:
> like $image_dir/bla.gif  ?

Yes, PHP won't guess where the image resides. ;-)

If you use the $image_dir variable within a function, don't forget to globalize it by putting
"global  $image_dir;" at the top of the function body:

function blabla (..) {

  global  $image_dir;
  ...
}

Best,

 sd

0
 
solutionDriverCommented:
Hi phillystyle123,

that's not hard to do.

Create a file config.php, and define the path to the image directory there, like:

<?php

$image_dir = "[absolute_path]/images/";

?>

Put in the absolute path instead of [absolute_path].

Then just include the config file in both index.php files:

In /index.php:

<?php

include 'config.php';

...
?>

In /subdirectory/index.php:

 <?php

include '../config.php';

...
?>

Best regards,

  sd
0
 
phillystyle123Author Commented:
hmm - i have it set up but it doesn't make sense to me - am i supposed to add that variable to each file path:

like $image_dir/bla.gif  ?

i've got this in my config file:

<?php

$image_dir = "usr/home/healthpresence/orthoneuro/images/";

?>

and then this at the top of both index.php files:

<?php include('includes/config.php');?> and <?php include(../'includes/config.php');?> but it's not working yet

0
 
phillystyle123Author Commented:
that's funny - i'd actually already done this in a slightly different way - thanks tons - much apprecated
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.