?
Solved

re: php relative paths to files using includes

Posted on 2009-04-29
4
Medium Priority
?
289 Views
Last Modified: 2012-05-06
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)
0
Comment
Question by:phillystyle123
  • 2
  • 2
4 Comments
 
LVL 5

Expert Comment

by:solutionDriver
ID: 24265826
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
 

Author Comment

by:phillystyle123
ID: 24265931
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
 
LVL 5

Accepted Solution

by:
solutionDriver earned 2000 total points
ID: 24265956

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
 

Author Closing Comment

by:phillystyle123
ID: 31576236
that's funny - i'd actually already done this in a slightly different way - thanks tons - much apprecated
0

Featured Post

Upgrade your Question Security!

Add Premium security features to your question to ensure its privacy or anonymity. Learn more about your ability to control Question Security today.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses
Course of the Month16 days, 18 hours left to enroll

864 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question