?
Solved

Universal path to a specific file or folder in php

Posted on 2009-04-27
7
Medium Priority
?
192 Views
Last Modified: 2013-12-12
I need to build a site on my server, and then changing to my client server with no need to change the paths, just to move the files.
Ex.: I have the config file in one folder, and the path for this template should work no matter the server is. Or I have the images folder for the galery on the root, and I neeed acess this folder from several pages, no matter where the site it is located.
0
Comment
Question by:WiseGoose
  • 4
  • 2
7 Comments
 
LVL 6

Expert Comment

by:basic612
ID: 24240869
For this you want to set up some variables in your script. By using the values of __FILE__ and      DIRECTORY_SEPARATOR constants, you should be able to include or require any files your applicaiton needs without knowing an absolute path.
0
 
LVL 6

Expert Comment

by:basic612
ID: 24240921
To clarify, as an example you could do something like this in your script:

<?php
 
 
$application_root_directory = __FILE__;
$includes_directory = $application_root_directory .  DIRECTORY_SEPARATOR . 'includes' .  DIRECTORY_SEPARATOR;
 
require($includes_directory . 'include_me.php');
 
?>

Open in new window

0
 

Author Comment

by:WiseGoose
ID: 24241426
Hi basic612,

Thanks. Suppose that I have a file called configure.php and I need to load it for all my scripts. This file is at the "includes" folder, wich is at a root level. I need to put the file at the top of all my scripts, and then change the server, without modify the script.
How do you adapt your script?

WiseGoose
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 3

Expert Comment

by:GodDoesntExist
ID: 24241892
You could also use the super global $_SERVER['DOCUMENT_ROOT']
0
 
LVL 6

Expert Comment

by:basic612
ID: 24241980
If all of your scripts are at the root level of your application apart from the configuration file which is in includes, you can just use relative paths:

include ('includes/configuration.php');

or for better portability:

include ('includes' . DIRECTORY_SEPARATOR . 'configuration.php');

I just find it simpler typing

$includes

instead of

'includes' . DIRECTORY_SEPARATOR

each time I want to call an include file.

I personally would avoid using the $_SERVER['DOCUMENT_ROOT'] global in case your application gets installed in a subfolder of the document root. You can;t control where the folder is placed and the scripts called from, however you can expect the relative structure of your directories and files to remain constant.
0
 

Author Comment

by:WiseGoose
ID: 24242250
Works fine your idea.
One last thing: there is a special reason for use a constant to the DIRECTORY_SEPARATOR?
Thanks.
0
 
LVL 6

Accepted Solution

by:
basic612 earned 750 total points
ID: 24246125
That is to ensure compatibility between Windows and Unix which use different separators:

// Linux:
DIRECTORY_SEPARATOR == '/'

// Windows:
DIRECTORY_SEPARATOR == '\'
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

It is possible to boost certain documents at query time in Solr. Query time boosting can be a powerful resource for finding the most relevant and "best" content. Of course the more information you index, the more fields you will be able to use for y…
This article discusses four methods for overlaying images in a container on a web page
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
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 Month15 days, 6 hours left to enroll

839 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