Solved

include path

Posted on 2015-02-16
6
52 Views
Last Modified: 2015-02-20
I have a file located here:
     mySite.com/DataTables/Editor/php/Bootstrap.php

There is a line that contains:
        include( dirname(__FILE__).'/config.php' );

But I need to use this for various projects that link to different databases and i don't think I want all the files in DataTables in each project folder.  So I need config to be in each project folder:
   mySite.com/Project1/lib/config.php
   mySite.com/Project2/lib/config.php


Instead of using  dirname(__FILE__) Is there a way that I can get to the correct project?  or is dirname going to return what I want?
0
Comment
Question by:UniqueData
  • 3
  • 2
6 Comments
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 40612851
What dirname() will return can be discerned via these pages.
http://php.net/manual/en/function.dirname.php
http://php.net/manual/en/language.constants.predefined.php

In the case with __FILE__, php says, "The full path and filename of the file with symlinks resolved. If used inside an include, the name of the included file is returned."
0
 
LVL 9

Expert Comment

by:Brian Tao
ID: 40613209
You can (at least) do one of the follwoing:
1) add your library directory to "include_path" directive in php.ini, or
2) without access to php.ini, use relative path in your code, for example, something like: include("../../../library/config.php");
0
 
LVL 7

Author Comment

by:UniqueData
ID: 40613454
but the thing is there will be multiple config files, one in each project folder, each with the connection string for the database for that project.
           
There is a  file is located in mySite.com/DataTables/Editor/php/Bootstrap.php, outside of any of the project folders, so that I don't have to copy all the DataTables files into each project folder.  Each project will have it's own page that has the statement:
      include( "../../DataTables/Editor/php/DataTables.php" );

This DataTables.php is the one with the statement:  include( dirname(__FILE__).'/config.php' );

So I need DataTables.php to know that it is being included from mySite.com/Project1/ or mySite.com/Project2/, etc.

So I guess, long story short:  how to the path of the file that called it?
0
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
LVL 7

Author Comment

by:UniqueData
ID: 40613468
just thought of another way to clarify (I hope)

www.mySite.com/Project1/lib/config.php (contains connection info for database that Project1 uses)
www.mySite.com/Project1/lib/getMathFacts.php

www.mySite.com/Project2/lib/config.php (contains connection info for a different database that Project2 uses)
www.mySite.com/Project2/lib/getStateCapitols.php

each of the "get" php files has the same line:
      include( "../../DataTables/Editor/php/DataTables.php" );

www.mySite.com/DataTables/Editor/php/DataTables.php needs to include the config file.
        So if it was called from Project1 it includes www.mySite.com/Project1/lib/config.php.  
        If it is called from Project2 it includes www.mySite.com/Project2/lib/config.php
0
 
LVL 7

Author Comment

by:UniqueData
ID: 40613477
oh my goodness.  I can't believe I didn't think of this...

right before I have my include DataTables I set a variable:
   $myPath = dirname(__FILE__), which would get the path of the current project

Then, in DataTables I use this in the include:
   include( $myPath.'/config.php' );
0
 
LVL 108

Accepted Solution

by:
Ray Paseur earned 500 total points
ID: 40613904
Then, in DataTables I use this in the include:
   include( $myPath.'/config.php' );
If you want a little more "portability" you may use the PHP constant DIRECTORY_SEPARATOR like this.  It's context-aware and will produce the correct type of slash for the operating system.

include( dirname(__FILE__) . DIRECTORY_SEPARATOR . 'config.php' );

In any case, be sure to read the man page carefully because PHP is not very clear about how it handles file paths!
http://php.net/manual/en/language.constants.predefined.php
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
htacces issue 2 40
PHP Variable into a number 3 34
update field on focusout 15 23
using hash in login 34 13
Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
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 …

757 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now