简体   繁体   English

函数在localhost上起作用,但在服务器上不起作用

[英]Function works on localhost but not on server

I made a function to get the main folder path in which website is stored. 我做了一个函数来获取存储网站的主文件夹路径。 In localhost it works fine. 在本地主机中,它可以正常工作。

function get_path()
{

    $current=dirname(__FILE__) . '/';   
    $name=basename(__DIR__);
    $from=array($name);
    $to=array('');
    $result=str_replace($from,$to,$current);    

return trim($result, "/\\");
}

But in server it shows error while including files. 但是在服务器中,在包含文件时显示错误。

 include(): Failed opening 'home3/home/public_html/dev/ship\model\main.php' for inclusion (include_path='.:/opt/php54/lib/php') 

The file is there in that directory for sure. 该文件肯定在该目录中。 But its not working. 但是它不起作用。

您正在去除第一个斜杠(第一个字符)-加上您在路径中使用\\而不是/。

Try the following 尝试以下

// 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;
}

Or you could use: 或者您可以使用:

// 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM