include_once, relative path in php

前端 未结 5 762
-上瘾入骨i
-上瘾入骨i 2021-01-18 02:36

I have 3 files: home, failed_attempt, login.

The file home and failed_attempt all refer to login file.

The annoying thing is that they throw a mistake saying

相关标签:
5条回答
  • 2021-01-18 02:38

    Do something like require(dirname(__FILE__) . '[RELATIVE PATH HERE]');

    0 讨论(0)
  • 2021-01-18 02:46

    Things like realpath() and __DIR__ are your friends when it comes to creating paths in PHP.

    Realpath http://php.net/manual/en/function.realpath.php

    Magic constants http://php.net/manual/en/language.constants.predefined.php

    Since include and require are language constructs (rather than functions) they don't need brackets. Generally speaking you'd use include to include "templates" (output files) and require to include PHP files such as classes.

    So you could use something like:

    $sPath = realpath( __DIR__ . '/../relative/path/here');
    if($sPath) { require_once $sPath; }
    

    (use dirname(__FILE__) instead of __DIR__ on PHP < 5)

    It's worth evaluating the path before attempting to require the file as realpath() returns false if the file doesn't exist and attempting to require false; spaffs out a PHP error.

    Alternatively you can just use absolute paths like:

    require /home/www/mysite.com/htdocs/inc/somefile.php

    0 讨论(0)
  • 2021-01-18 02:48

    Here are three possible solutions. The second are really just work-arounds that use absolute paths in a clever way.

    1: chdir into the correct directory

    <?php
    
    // check if the 'StoredProcedure' folder exists in the current directory
    // while it doesn't exist in the current directory, move current 
    // directory up one level.
    //
    // This while loop will keep moving up the directory tree until the
    // current directory contains the 'StoredProcedure' folder.
    //
    while (! file_exists('StoredProcedure') )
        chdir('..');
    
    include_once "StoredProcedure/connect.php";
    // ...
    ?>
    

    Note that this will only work if your StoredProcedure folder is in the topmost directory of any files that might need to include the files it contains.

    2: Use absolute paths

    Now before you say this is not portable, it actually depends on how you implement it. Here's an example that works with Apache:

    <?php
    include_once $_SERVER['DOCUMENT_ROOT'] . "/StoredProcedure/connect.php";
    // ...
    ?>
    

    Alternatively, again with apache, put the following in your .htaccess in the root directory:

    php_value auto_prepend_file /path/to/example.php
    

    Then in example.php:

    <?php
    
    define('MY_DOC_ROOT', '/path/to/docroot');
    
    ?>
    

    And finally in your files:

    <?php
    include_once MY_DOC_ROOT . "/StoredProcedure/connect.php";
    // ...
    ?>
    

    3: Set PHP's include_path

    See the manual entry for the include_path directive. If you don't have access to php.ini, then this can be set in .htaccess, providing you are using Apache and PHP is not installed as CGI, like so:

    php_value include_path '/path/to/my/includes/folder:/path/to/another/includes/folder'
    
    0 讨论(0)
  • 2021-01-18 02:51

    For the beginners this will help to understand. Use simply the following based on your relative path:

    //include file directly from parent directory:
    include_once(dirname(__FILE__) . '/../connect.php');
    

    OR

    //include file from parent's child directory:
    include_once(dirname(__FILE__) . '/../StoredProcedure/connect.php');
    

    OR

    //include file from own child directory:
    include_once('StoredProcedure/connect.php');
    

    OR

    //include sibling files from same directory:
    include_once('connect.php');
    
    0 讨论(0)
  • 2021-01-18 02:58

    The other guys have answered your question correctly, however I thought I'd contribute something else: autoloaders.

    Autoloaders allow you to define a function that will automatically include certain files, when you attempt to use a class.

    The documentation is here and can explain it better than I can: http://php.net/manual/en/language.oop5.autoload.php

    Would really recommend using them, will save you time and is better practice.

    0 讨论(0)
提交回复
热议问题