免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1880 | 回复: 2
打印 上一主题 下一主题

php有获得绝对地址的函数吗? [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2006-05-14 00:13 |只看该作者 |倒序浏览
比如想获得

http://csdn.net/expert/001/./../../example.php

的绝对地址
http://csdn.net/example.php

再者,具体容错性

http://csdn.net/../../../../index.php
获得其的真实地址http://csdn.net/../../../../index.php

想得到上述答案该用什么函数呢?

论坛徽章:
1
技术图书徽章
日期:2013-12-05 23:25:45
2 [报告]
发表于 2006-05-14 11:54 |只看该作者
这个相对的关系恐怕不是那么好获得的!

一个简单的例子:如果服务器设置了虚拟目录怎么办?
或者符号链接怎么办?

一般自己写一个析构就可以分析了。

论坛徽章:
0
3 [报告]
发表于 2006-05-14 12:45 |只看该作者
手册上抄一段给你,应该可用.


  1. carlosreche at yahoo dot com
  2. 16-Dec-2004 07:52
  3. In my last function, it will always discard "../" if there are no more parent dirs to go back. But it's a mistake! For example, relative paths like "../../downloads" returns "downloads/", and it should return exactly the same entry (because parent directories for "downloads" may exist in this case). Well, I've corrected this bug. Now:

  4. "C:/downloads/../../../" returns "C:/"
  5. "downloads/../../../"  returns  "../../"

  6. <?php

  7. function real_path($path)
  8. {
  9.    if ($path == "")
  10.    {
  11.        return false;
  12.    }

  13.    $path = trim(preg_replace("/\\\\/", "/", (string)$path));

  14.    if (!preg_match("/(\.\w{1,4})$/", $path)  &&  
  15.        !preg_match("/\?[^\\/]+$/", $path)  &&  
  16.        !preg_match("/\\/$/", $path))
  17.    {
  18.        $path .= '/';
  19.    }

  20.    $pattern = "/^(\\/|\w:\\/|https?:\\/\\/[^\\/]+\\/)?(.*)$/i";

  21.    preg_match_all($pattern, $path, $matches, PREG_SET_ORDER);

  22.    $path_tok_1 = $matches[0][1];
  23.    $path_tok_2 = $matches[0][2];

  24.    $path_tok_2 = preg_replace(
  25.                    array("/^\\/+/", "/\\/+/"),
  26.                    array("", "/"),
  27.                    $path_tok_2);

  28.    $path_parts = explode("/", $path_tok_2);
  29.    $real_path_parts = array();

  30.    for ($i = 0, $real_path_parts = array(); $i < count($path_parts); $i++)
  31.    {
  32.        if ($path_parts[$i] == '.')
  33.        {
  34.            continue;
  35.        }
  36.        else if ($path_parts[$i] == '..')
  37.        {
  38.            if (  (isset($real_path_parts[0])  &&  $real_path_parts[0] != '..')
  39.                || ($path_tok_1 != "")  )
  40.            {
  41.                array_pop($real_path_parts);
  42.                continue;
  43.            }
  44.        }

  45.        array_push($real_path_parts, $path_parts[$i]);
  46.    }

  47.    return $path_tok_1 . implode('/', $real_path_parts);
  48. }

  49. ?>
复制代码
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP