Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
542 views
in Technique[技术] by (71.8m points)

php - Functions stop working when file included with root path (leading slash)

My PHP files in my root directory INCLUDE header.php. Header.php INCLUDEs functions.php. I'm adding new pages in a subdirectory, so I added leading slashes to all my links in header.php: CSS, menu items and the subsequent INCLUDE to functions.php. The CSS and menu items work fine on this page in the subdirectory, but the functions don't work. There are no links in the functions that seem to need leading slashes.

Does the combination of include and leading slashes require modifying functions?

From a page in the root directory:

include('header.php');

From a page in the subdirectory:

include('/header.php');

From header.php:

include('/functions.php');

And the function that no longer works (called from pages in the root directory or subdirectory):

function show_date($array_name){
if (date("Y F j",strtotime($array_name["exhibit_open"])) == date("Y F j",strtotime($array_name["exhibit_close"]))){
    echo date("F j, Y",strtotime($array_name["exhibit_open"]));
}
elseif (date("Y",strtotime($array_name["exhibit_open"])) != date("Y",strtotime($array_name["exhibit_close"]))) {
    $first_date_format = "F j, Y";
    echo date($first_date_format,strtotime($array_name["exhibit_open"])). " - ". date("F j, Y",strtotime($array_name["exhibit_close"]));
} elseif (date("F",strtotime($array_name["exhibit_open"])) != date("F",strtotime($array_name["exhibit_close"]))){
    $first_date_format = "F j";
    echo date($first_date_format,strtotime($array_name["exhibit_open"])). " - ". date("F j, Y",strtotime($array_name["exhibit_close"]));
} else {
    $first_date_format = "j";
    echo date("F j",strtotime($array_name["exhibit_open"])). " - ". date($first_date_format,strtotime($array_name["exhibit_close"])). ", ". date("Y",strtotime($array_name["exhibit_close"]));
}

}

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Standard Paths 101:

/path/somefile - the leading / anchors this path structure at the ROOT of the file system, e.g. it's the equivalent of C:pathsomefile.

path/somefile - no leading /. The OS will use the programs "current working" directory as the basis of the path, so if you're in a shell that's in /home/foo, then somefile will be search for in /home/foo/path/somefile.

../somefile. the .. refers to the PARENT directory of the current working directory, so if you're in /home/foo, then ../somefile will be searched for as /home/somefile.

Note that you can have non-sensical paths like

/../../../../somefile. This will be acceptable, but is pointless, as you're both anchoring the path at the root of the file system, then trying to go ABOVE the root, which is not possible. This path is the operational equivalent of /somefile.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...