0

I need to include a path from a config file in one directory on other scrpits a few directories up or down.

My config file has a function like this:

$execute_path = getcwd() . "/execute.php";

The execute.php file resides in the same directory as the config file wich makes it ok and the output will become:

public-html/config-directory/execute.php

Now if use the same function in a directory that is outside of the original directory, it treats the getcwd() function as if it was in that directory.

For example, if call it from a directory public-html/one/two/three/somefile.php as follow:

<?php 
include(pathto/config.php)
echo $execute_path;
?>

The output becomes publichtml/one/two/three/execute.php rather than staying publichtml/config-directory/execute.php

How can I achieve what I want here? Sorry for the long explanation. Thanks.

Chris81
  • 426
  • 2
  • 4
  • 16

1 Answers1

1

Include can be very tricky why i advice is

//config.php

define("SITE_PATH", "public-html/config-directory");


//execute.php

$execute_path =SITE_PATH . "/execute.php";

//run.php
include_once 'config.php';
include_once 'execute.php';

echo $execute_path;
Baba
  • 94,024
  • 28
  • 166
  • 217
  • The problem is that config file is generated based on the users server setting, and wherever they uploaded the main (it's a plugin) directory. Which means defining the path manually in the config fie is not going to work. – Chris81 Apr 27 '12 at 16:58
  • 1
    Its safer since because its config and you would not be using it in working directory all the time `getcwd()` would only return current working directory – Baba Apr 27 '12 at 17:00
  • I could use the getcwd when the config file is being written and use it as you describe. Instead of having a function it would become a path so the cofig would read: $execute_path = "path/execute.php" – Chris81 Apr 27 '12 at 17:03
  • its not a function ... it a constant .. there are so many ways to get working directory `__DIR__` , `basename(__FILE__)` etc .. but for a config file ... no no no .. you need a base – Baba Apr 27 '12 at 17:09
  • So i'm using a form to get the user input, and the post to the config file. How can i get the complete path and add the file name and post it to the config? – Chris81 Apr 27 '12 at 17:19
  • 1
    `basename(__DIR__) . "/config.php` that would get parent dir or `basename(__FILE__) . "/config.php` current dir – Baba Apr 27 '12 at 17:23
  • basename(__DIR__) . "/config.php"; works great but it only get the path up to the base, and not all the way from the start (public-html blah blah). Is there any way that i can get the complete path? thanks a million for all your help. I really appreciate it. – Chris81 Apr 27 '12 at 17:31
  • 1
    run the base name twice .. you know i don't know your current settings ..but i would advice but the know the installation path use that to `basename` or `dirname` to the config path then save the settings – Baba Apr 27 '12 at 17:35
  • I ended up doing this way `$dir = dirname(__FILE__) . "/execute.php";` By including this in the config file at the time of creation. Thanks again for your help. – Chris81 Apr 27 '12 at 18:48