0

I have a problem with my include path. I am trying to build a cms but I want to build it so that not all the files need to be in the root for the including.

Link To File Structure

I am trying to include from anywhere to an other file. But the problem is that I include files in other files.

Like: I include ErrorHandling in T_ErrorLog But i use A textfile in Textfiles in ErrorHandling so that path isn't correct.

Errors:

Notice: Undefined variable: error in C:\Users\Kevin\Desktop\Dropbox\Programming\Webdesign\Workspace\KLJ Peer\GhostCMS\TestFiles\T_ErrorLog.php on line 7
Test

Warning: fopen(/GhostCMS/Textfiles/ErrorLog.txt) [function.fopen]: failed to open stream: No such file or directory in C:\Users\Kevin\Desktop\Dropbox\Programming\Webdesign\Workspace\KLJ Peer\GhostCMS\Core\ErrorHandling.php on line 15
can't open file

T_ErrorLog.php

<?php

include_once ('../Core/ErrorHandling.php');

use Core\ErrorHandling;

ErrorHandling::HandleError("Test", $error);

ErrorHandling.php

namespace Core;

class ErrorHandling {
    public static function HandleError($errorMessage, $error){
        echo $errorMessage . "<br />";
        echo "Indien dit zicht blijft voor doen.<br />";
        echo "Zal de webmaster dit ontvangen en even kijken naar het probleem!<br />";

        ErrorHandling::WriteToErrorFile($errorMessage, $error);
    }
    public static function WriteToErrorFile($errorMessage, $error){
        $myFile = "/GhostCMS/Textfiles/ErrorLog.txt";
        $fh = fopen($myFile, 'a') or die("can't open file");

        $today = date('j/n/Y - H:i:s');
        fwrite($fh, "Error: \n");
        fwrite($fh, "Date: " . $today . "\n");
        fwrite($fh, "User error message: " . $errorMessage . "\n");
        fwrite($fh, "System error message: " . $error . "\n");
        fwrite($fh, "--------------------------------------------------\n");
        fclose($fh);
    }
}

Is there some way to make the include path relative for al the directories???

kevingoos
  • 3,785
  • 4
  • 36
  • 63
  • `$error` in T_Logfile.php is not defined – tawfekov May 29 '12 at 10:53
  • Problem solved for the $error :D Test Indien dit zicht blijft voor doen. Zal de webmaster dit ontvangen en even kijken naar het probleem! Warning: fopen(/GhostCMS/Textfiles/ErrorLog.txt) [function.fopen]: failed to open stream: No such file or directory in C:\Users\Kevin\Desktop\Dropbox\Programming\Webdesign\Workspace\KLJ Peer\GhostCMS\Core\ErrorHandling.php on line 15 can't open file – kevingoos May 29 '12 at 10:58

2 Answers2

1

Well you know your file structure and it won't really change. Generally speaking you have a few options: record the root directory or starting point from where to derive your other files to include with options like DIR or FILE PHP Magic Constants

e.g. you could have saved FILE upon install or in your bootstrap file and then just adapt directories to derive from that path.

$myPreviouslySavedPath = __DIR__   // e.g. /home/user/meme/www/
fopen($myPreviouslySavedPath . '/GhostCMS/Textfiles/ErrorLog.txt') // would then be /home/user/meme/www/GhostCMS/Textfiles/ErrorLog.txt 

Alternatively for php includes php has an autoloader, which is just a function you supply yourself, where you let that function decide where to load the specific file from. This is something you'd set in the entrypoint of your CMS and would probably combine with the base directory you collected.

e.g.

spl_autoload_register('myClassLoader');

function myClassLoader($class){
     // << here you'd place logic to decide the best path for your file >>
     include(__DIR__ . "/classes/$class");
}

The main thing to remember and use is that you have a fixed entrypoint for your application and you know the location of that file. Therefrom you can deduce the logical path of all other files.

Harald Brinkhof
  • 4,375
  • 1
  • 22
  • 32
0

I would recommend to rewrite the code so that no include or require is used and this approach can be achieved by right setting Your include path - see http://php.net/set_include_path and http://php.net/get_include_path and using an autoloader class/function.

Then when 'using' namespace based classes Your autoloader will search through include path and try to include that files...

By this You can have external libraries, classes in different place then Your CMS but still be able to include them easily by autoloader and namespaces...

shadyyx
  • 15,825
  • 6
  • 60
  • 95