Jump to content

Recommended Posts

Hey everyone. I'm completely stuck with this thing. I've tried several ways to make things work, but they all look really ugly to me.

 

The problem is basically the following. Let's say I have the following directory structure:

 

/

--- index.php

--- include

------- connection.php

------- user.php

--- administration

------- index.php

 

So I want to create the following includes:

 

/index.php --> connection.php, user.php

/include/user.php --> connection.php

/administration/index.php --> connection.php, user.php

 

I've tried the following methods:

 

1.

if(file_exists("include/connection.php"))include("include/connection.php");
else include("../include/connection.php");

 

2.

while(!file_exists("include/connection.php"))changedir("..");

 

3.

while(!file_exists("root.path"))changedir("..");

 

4.

// create a file .relative with the number of folders to go up to get to the root
$number = file_get_contents(".relative");
for($i = 0; $i<$number;$i++)changedir("..");

 

And even

5.

include($_SERVER['DOCUMENT_ROOT'])."/include/connection.php");

 

The problem is, 1-4 look very untidy to me and code may get too complicated to edit in the future. The problem with 5, is that the application will not necessarily be hosted on the document root.

 

What i want, however, is to make sure that the current directory is always set to a given directory, let's say /var/www/mysite.com/. This should work even if i lack administrative privileges on the server.

 

I've searched the web but I couldn't really find a solution to this problem.

 

Thanks in advance

What's wrong with this?

 

include './include/connection.php';
include './include/user.php';

 

The problem is that some files, that I will include will have references to other files.

 

Like this:

 

Running index.php would set the current directory to the application root, so if index.php includes "include/user.php", and user.php includes "./include/connection.php" it will work, why? because the current directory is /

 

However, if I run /administration/index.php and include "../include/user.php", the current directory will be set to /administration and when user.php attempts to include "./include/connection.php" it will fail to find the file, because the current directory has changed.

 

Thanks for such a quick reply!

the application will not necessarily be hosted on the document root

 

Use #5 but also use an 'application path' variable (or defined constant) in it that you set/define in your application configuration (config.php) file. Something like -

 

include $_SERVER['DOCUMENT_ROOT'] ."/$app_path/include/connection.php";

 

You could also add entries to the include_path using set_include_path() and let php search for the files, but if you have a large number of files and several different include locations, performance will be better by specifying the actual path to the files being included.

the application will not necessarily be hosted on the document root

 

Use #5 but also use an 'application path' variable (or defined constant) in it that you set/define in your application configuration (config.php) file. Something like -

 

include $_SERVER['DOCUMENT_ROOT'] ."/$app_path/include/connection.php";

 

You could also add entries to the include_path using set_include_path() and let php search for the files, but if you have a large number of files and several different include locations, performance will be better by specifying the actual path to the files being included.

 

That's a good idea. However, how can I include my config.php? How to find it?

set the include path

ini_set("include_path",($_SERVER['DOCUMENT_ROOT']."/your/include/path".PATH_SEPARATOR.ini_get("include_path"))); 

 

then it doesn't matter what the current folder is it will look in the include folder.  you can add more folders to this also.

 

 

no it's a php function so it doesn't require special permissions. This is how I do it.

 

 

at the top of each page.

require_once 'includes/setpath.php';    // '../includes/setpath.php';     depending on what folder I'm in 

the contents of setpath.php would be something along these lines.

ini_set("include_path",($_SERVER['DOCUMENT_ROOT']."/your/include/path".PATH_SEPARATOR.ini_get("include_path"))); 

 

no it's a php function so it doesn't require special permissions. This is how I do it.

 

 

at the top of each page.

require_once 'includes/setpath.php';    // '../includes/setpath.php';     depending on what folder I'm in 

the contents of setpath.php would be something along these lines.

ini_set("include_path",($_SERVER['DOCUMENT_ROOT']."/your/include/path".PATH_SEPARATOR.ini_get("include_path"))); 

 

Excellent.

 

Gotta see if that works! Thanks!!

I've found the following quite useful as well.

dirname( __FILE__ ) 

to get the "current" directory of the file you're executing this in, add more dirname functions around it if you need to move up a folder.

 

Cool! This function could come in handy!

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.