Site-root relative path doesn't work

后端 未结 5 765
死守一世寂寞
死守一世寂寞 2021-01-27 21:37

I\'m not understanding this. It\'s screwing up whole site because I\'m using a php template.

Supposedly beginning a link with \'/\' starts me at the root according to ev

相关标签:
5条回答
  • 2021-01-27 22:09

    Why don't you start from where index.php is? Like this:

    <?php  
      require('textfiles/php/variables.php');  
      $currentpage = $page[0];  
      require('textfiles/php/template.php');  
    ?>
    

    And for other pages use:

    <?php  
      require('../php/variables.php');  
      require('../php/template.php');  
    ?>
    
    0 讨论(0)
  • 2021-01-27 22:16

    Stop worrying about relative path by just using a base path set as a configuration so you are not constantly juggling relative locations. For example, in your main config file, you can define a base path like this:

    $BASE_PATH = '/the/path/to/the/codebase/';
    

    If you don’t know the base path to your files, then place this line at the top of your PHP code:

    echo "Your path is: " . realpath(dirname(__FILE__)) . "<br />";
    

    And load that page. Somewhere near the top should be a line that reads:

    Your path is: /the/path/to/the/codebase/

    Of course /the/path/to/the/codebase/ will be your actual file path, but that will be your base path. Then just set $BASE_PATH to that value.

    Then when you do an require, the syntax would be:

    require($BASE_PATH . '/cis130/textfiles/php/variables.php');
    

    The benefit of this is no matter how deeply nested your codebase becomes, you will always be anchored to the value of $BASE_PATH. And your life will be made tons easier thanks to not having to worry about relative path issues.

    I would also recommend using require_once instead of require to avoid scenarios where your script might inadvertently attempt to load the same file more than once.

    require_once($BASE_PATH . '/cis130/textfiles/php/variables.php');
    
    0 讨论(0)
  • 2021-01-27 22:19

    I think the easiest way to fix this would be to set your include directory and put all of your files in that.

    In your php.ini file, find this line...

    ini_set('include_path', '/usr/lib/pear');
    

    ...but exchange '/usr/lib/pear' with the path to the folder you would like to keep your documents in. It can be above or below the root directory.

    Then, put all of your include files in there. No matter what level of the directory you are including from, it will look in that directory for them, making include files easy to write.

    Or, you could set the folder path right there in the fuel you are updating. Make sure this is at the top of any php file that has an include. All of your require() functions will look in there first.

    <?php
    set_include_path('/path/to/folder');
    
    0 讨论(0)
  • 2021-01-27 22:22

    Try this:

    require($_SERVER['DOCUMENT_ROOT'].'/cis130/textfiles/php/variables.php');
    

    $_SERVER['DOCUMENT_ROOT'] is how you specify the root directory in php.

    0 讨论(0)
  • 2021-01-27 22:22

    In linux, URIs starting with '/' are full paths. Example, '/run/shm/', because / is like saying C: in Windows.

    Writing 'dir/something.php' should work, if your PHP file is in the parent dir of dir.

    Also, you can use __DIR__ to refer to the current directory.

    Example: $file = __DIR__.'filename.php'

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

    URLs starting with '/' are relatives to the site's root but only in client-side, html. Example, you can link something: <a href="/contact">Contact us</a>

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