views:

142

answers:

1

I was wondering if there is a way of making a relative path to the main folder (where the index.php lies) from every file that needs to be included or requested by AJAX.

I want to combine both AJAX and PHP include so first time the page loads with PHP, and then to be able to refresh parts of the page with AJAX, but the files are the same and lie in subfolders.

I'm having problems with the path and although I can set an absolute path, then I have to change it every time the server changes. I want a relative path to where my project is, but not DOCUMENT_ROOT, because that one doesn't work with aliases. (or do you know how to make it work with aliases? )

Thanks !

EDIT* Here is how my directory structure looks:

[main_dir]/index.php - the main index file

[main_dir]/phpinc - the php include files - that work only with include (no AJAX call on them)

[main_dir]/modules - the modules are parts of the page , can be either included with PHP or AJAX called from anywhere (doesn't matter, they will be loaded as an individual HTML file, so the paths in PHP are relative to where the module is phisically situated.

When you include them in PHP, they will normally take the [main_dir] path (because index.php has included them)

So my problem is: from the module files I want to include some files in the phpinc folder, but it could either be ../phpinc/file.php or phpinc/file.php .

Is there a more elegant way than just putting an

if (is_dir("phpinc")) {
 $inc_path = "phpinc";   
} else if (is_dir("../phpinc")) {
 $inc_path = "../phpinc";
}

at the beginning of each module?
Hope this clarification is good enough.

A: 

UPDATED:

try to follow me:

i have this App structure

/realpath/
 - phpinc/
   - file.php
   - config.php
 - module/
   - module1.php
   - module2.php
 - index.php

realpath is the main dir where our App reside!

now

index.php have this code:

<?php
// this is your save life, must stay always on the top of main index
// OR into each page that are not included in the index file!
include ( 'phpinc/file.php' );

include ( MODULE_PATH . 'module1.php' );

echo '<script>'; 
 // now assuming you are running AJAX from here!    
 // you don't need to warry about the path!
echo '</script>'; 

echo '<p>Hello World!<p>';
echo APP_PATH; # /user/root/realpath/
echo '<br />'; 
echo PHPINC_PATH; # /user/root/realpath/phpinc/
echo '<br />';
echo PHPINC; # /user/root/realpath/phpinc/file.php
echo '<br />';
echo MODULE_PATH; # /realpath/module/
echo '<br />';
echo $html; # 555-555-555   aSeptik

?>

file.php have this code:

<?php
define ( APP_PATH , getcwd() .'/' );
define ( MODULE_PATH , APP_PATH . '/module/' );
define ( PHPINC_PATH , dirname(__FILE__) .'/' );
define ( PHPINC , realpath(__FILE__) );

?>

module1.php have this code:

<?php
include ( PHPINC_PATH . 'config.php' );

$html .= '<table>';
$html .= '<tr>';
$html .= '<td>'.$password.'</td>';
$html .= '<td>'.$username.'</td>';
$html .= '</tr>';
$html .= '</table>';
?>

config.php have this:

$password = '555-555-555';
$username = 'aSeptik';

As you can see you need to have the file.php always on top and declared only once for all descendant included file!

you now have your own path definition for resue as you need in your App!

Let me know!

aSeptik
thanks for the answer. I can't however see a solution in using javascript (I have no problems with the paths in JS) See the question description for some more details I added.
Cristian
The main_dir changes when I change from development server to final server. So I want something that will not depend on the absolute paths. Just to be relative to the inside of the [main_dir]
Cristian
one more update! ;-)
aSeptik
thanks. It doesn't however fit to what I'm trying to do. I'm not sure I explained very clearly as it's a complicated issue I'm trying to find answer to. But it's nice to learn about that realpath('.') function. I still have to compare something if the file lies in the modules folder or if it is being called by index.php. That issue is solved by adding that code I shown to each file in the modules folder. But just wondering if it could be done easier.
Cristian
see update, hope it can help some way! ;-)
aSeptik
doesn't max just return the biggest string?
Cristian
for me work try to just create a test page put the function in it and **echo public_base_directory();**
aSeptik
when I run it from a subfolder of my main directory, it doesn't output the main, just the subfolder.
Cristian
Cristian
Don't warry Cristian! happy you find the way! although instead of putting the if statement on each file, place it in one php and include it in every file! ;-)
aSeptik
that is my problem! I can't do that because I don't know what the path of the initial file that loads other files. That's because I use both AJAX and PHP include. So no includes until I do the if ... that's copy paste for me the only solution here...Do you get what I was trying to do?
Cristian
updated! read and let me know!
aSeptik
Thanks for your elegant solution. However it does not apply in my case. I'll give you one example: think of module1.php as loaded individually, as a stand-alone script. Then it won't know of any defined constants. That's how AJAX is loading it, making the .php think it's a standalone launch (not from any include). So the module doesn't have any connection with any other scripts, except from the parameters and arguments you put in. So I think that the only acceptable solution is to have that if in every module. Thanks for you support!
Cristian