首页 > 代码库 > PHP 去掉路径中的 "/./" 或者 “/../”

PHP 去掉路径中的 "/./" 或者 “/../”

<?php$filename = ‘/mnt/hgfs/workspace/weituibao/app/config/../../public/files/36/temp/201410/21/1413873853598.jpg‘;function getRealPath($filename, $split = ‘/‘){    while(true) {        if (FALSE === strpos($filename, $split . ‘.‘)) {            break;        }        $filename = explode($split, $filename);        foreach($filename as $k => $f) {            if (($k && $f == ‘‘) || $f == ‘.‘) {                unset($filename[$k]);
          break; }
elseif ($f == ‘..‘) { unset($filename[$k]); if(isset($filename[$k-1])) unset($filename[$k-1]); break; } } $filename = implode($split, $filename); } return $filename;}echo getRealPath($filename);

 

---------- php ----------
/mnt/hgfs/workspace/weituibao/public/files/36/temp/201410/21/1413873853598.jpg
输出完成 (耗时 0 秒) - 正常终止

 

PHP 去掉路径中的 "/./" 或者 “/../”