$SERVER['HTTP_REFERER']可以得到文件的脚本路径如:http://localhost/test/(这一路径是根据脚本入口)但是我想要得到的是,不管这个文件是从什么位置被引入,都能得到这一脚本路径就好像__FILE__一样得到一个绝对路径,不随引入位置而改变有没有办法,路过的大侠指教

解决方案 »

  1.   

    $SERVER['SCRIPT_FILENAME']  这个吗?
      

  2.   

    realpath只是随目录改变而改变的真实的目录,要绝对路径的话一般的框架里都有用常量定义的。楼主可以用$_SERVER[SERVER_NAME]取得网站的根目录,在哪个文件夹后面再加个文件夹就行了。
      

  3.   

    我是想得到服务器路径,如:http://localhost/test/而不是 E:/wwwroot/test/这样的路径SERVER_NAME是得到的服务器名,而不是路径
    realpath是处理路径,而不是得到路径的方法
    SERVER_FILENAME得到的是绝对路径,但那不是我想要的我想得到的是如http://localhost/test/下文件index.php
    不管是它上层,或是下层目录,在调用这个文件的时候,其中的路径是不变的(img.src)都可以正确显示
    像这种:E://wwwroot/test/       img是不支持的,显示不了
      

  4.   

    $_SERVER里的东西
    给你个joomla的代码片断if (isset($_SERVER['HTTPS']) && !empty($_SERVER['HTTPS']) && (strtolower($_SERVER['HTTPS']) != 'off')) {
    $https = 's://';
    } else {
    $https = '://';
    } /*
     * Since we are assigning the URI from the server variables, we first need
     * to determine if we are running on apache or IIS.  If PHP_SELF and REQUEST_URI
     * are present, we will assume we are running on apache.
     */
    if (!empty ($_SERVER['PHP_SELF']) && !empty ($_SERVER['REQUEST_URI'])) { /*
     * To build the entire URI we need to prepend the protocol, and the http host
     * to the URI string.
     */
    $theURI = 'http' . $https . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']; /*
     * Since we do not have REQUEST_URI to work with, we will assume we are
     * running on IIS and will therefore need to work some magic with the SCRIPT_NAME and
     * QUERY_STRING environment variables.
     */
    }
     else
     {
    // IIS uses the SCRIPT_NAME variable instead of a REQUEST_URI variable... thanks, MS
    $theURI = 'http' . $https . $_SERVER['HTTP_HOST'] . $_SERVER['SCRIPT_NAME']; // If the query string exists append it to the URI string
    if (isset($_SERVER['QUERY_STRING']) && !empty($_SERVER['QUERY_STRING'])) {
    $theURI .= '?' . $_SERVER['QUERY_STRING'];
    }
    } // Now we need to clean what we got since we can't trust the server var
    $theURI = urldecode($theURI);
    $theURI = str_replace('"', '"',$theURI);
    $theURI = str_replace('<', '&lt;',$theURI);
    $theURI = str_replace('>', '&gt;',$theURI);
    $theURI = preg_replace('/eval\((.*)\)/', '', $theURI);
    $theURI = preg_replace('/[\\\"\\\'][\\s]*javascript:(.*)[\\\"\\\']/', '""', $theURI);

    echo $theURI;
      

  5.   

    用 $_SERVER['DOCUMENT_ROOT'] 再自己拼凑一下吧!
      

  6.   

    不知道你到底想要什么
    给你一些参考的
    realpath得到物理绝对路径
    $_SERVER['DOCUMENT_ROOT'] 可得到如 http://example.org/ 域名跟目录的物理路径,可同时配合 realpath 得到绝对物理路径可通过以上两者计算得到不少东西了另外如果想根据域名跟目录调用,链接或者图片地址可以使用 /images/xx.png   /js/script.js 这样的以 / 开头的路径即表示从域名跟目录开始定位,不论你当前访问的是在哪里如 http://example.org/sub1/1.html 调用了 1.png 则实际为 http://example.org/sub1/1.png
    若调用 /1.png 则实际为 http://example.org/1.png
      

  7.   

    #8楼最终的$theURI,你再利用php函数parse_url处理一下,既可得项目http绝对路径