我制作了一个函数来获取存储网站的主文件夹路径。在localhost中,它运行良好。
function get_path()
{
$current=dirname(__FILE__) . '/';
$name=basename(__DIR__);
$from=array($name);
$to=array('');
$result=str_replace($from,$to,$current);
return trim($result, "/''");
}
但在服务器中,它在包含文件时显示错误。
include(): Failed opening 'home3/home/public_html/dev/ship'model'main.php' for inclusion (include_path='.:/opt/php54/lib/php')
文件肯定在那个目录里。但它不起作用。
尝试以下
// Define directory separator
define('DS', DIRECTORY_SEPARATOR);
function get_path()
{
$current = dirname(__FILE__) . DS;
$name = basename(__DIR__);
$from = array($name);
$to = array('');
$result = str_replace($from, $to, $current);
return $result;
}
或者你可以使用:
// define directory separator
define('DS', DIRECTORY_SEPARATOR);
function get_path($withSlash = true)
{
$path = realpath(dirname(__FILE__));
if ($trailingSlash) {
$path .= DS;
}
return $path;
}
您去掉了第一个斜杠(第一个字符),并且在路径中使用''而不是/。
对于路径的创建,您应该使用PHP函数realpath来管理斜杠:http://php.net/realpath