How do I set an absolute include path in PHP?

后端 未结 10 1875
别那么骄傲
别那么骄傲 2020-12-04 11:32

In HTML, I can find a file starting from the web server\'s root folder by beginning the filepath with "/". Like:

/images/some_image         


        
相关标签:
10条回答
  • 2020-12-04 11:59

    The include_path setting works like $PATH in unix (there is a similar setting in Windows too).It contains multiple directory names, seperated by colons (:). When you include or require a file, these directories are searched in order, until a match is found or all directories are searched.

    So, to make sure that your application always includes from your path if the file exists there, simply put your include dir first in the list of directories.

    ini_set("include_path", "/your_include_path:".ini_get("include_path"));
    

    This way, your include directory is searched first, and then the original search path (by default the current directory, and then PEAR). If you have no problem modifying include_path, then this is the solution for you.

    0 讨论(0)
  • 2020-12-04 11:59

    There is nothing in include/require that prohibits you from using absolute an path. so your example

    include('/includes/header.php'); 
    

    should work just fine. Assuming the path and file are corect and have the correct permissions set.
    (and thereby allow you to include whatever file you like, in- or outside your document root)

    This behaviour is however considered to be a possible security risk. Therefore, the system administrator can set the open_basedir directive.

    This directive configures where you can include/require your files from and it might just be your problem.
    Some control panels (plesk for example) set this directive to be the same as the document root by default.

    as for the '.' syntax:

    /home/username/public_html <- absolute path  
    public_html <- relative path  
    ./public_html <- same as the path above  
    ../username/public_html <- another relative path  
    

    However, I usually use a slightly different option:

    require_once(__DIR__ . '/Factories/ViewFactory.php');
    

    With this edition, you specify an absolute path, relative to the file that contains the require_once() statement.

    0 讨论(0)
  • 2020-12-04 12:02

    Thanks - this is one of 2 links that com up if you google for php apache windows absolute path.

    As a newbie to intermed PHP developer I didnt understand why absolute paths on apache windopws systems would be c:\xampp\htdocs (apache document root - XAMPP default) instead of /

    thus if in http//localhost/myapp/subfolder1/subfolder2/myfile.php I wanted to include a file from http//localhost/myapp

    I would need to specify it as: include("c:\xampp\htdocs\myapp\includeme.php") or include("../../includeme.php")

    AND NOT include("/myapp/includeme.php")

    0 讨论(0)
  • 2020-12-04 12:12

    hey all...i had a similar problem with my cms system. i needed a hard path for some security aspects. think the best way is like rob wrote. for quick an dirty coding think this works also..:-)

    <?php
    $path   = getcwd(); 
    $myfile = "/test.inc.php";
    
    /* 
    

    getcwd () points to: /usr/srv/apache/htdocs/myworkingdir (as example)

    echo ($path.$myfile);
    would return...
    
    /usr/srv/apache/htdocs/myworkingdir/test.inc.php
    
    access outside your working directory is not allowed.
    */
    
    
    includ_once ($path.$myfile);
    
    //some code
    
    ?>
    

    nice day strtok

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