首页 > 编程知识 正文

php realpath报错替换PHP的realpath,替换jar包报错

时间:2023-05-04 04:00:28 阅读:264932 作者:2483

显然,realpath是非常错误的.在PHP 5.3.1中,它会导致随机崩溃.

在5.3.0及更低版本中,realpath随机失败并返回false(当然是相同的字符串),而且它总是在两次/更多次实际路径上失败(当然,它第一次工作).

此外,它在早期的PHP版本中是如此的错误,它完全无法使用.嗯……它已经是,因为它不一致.

无论如何,我有什么选择?也许自己重写一下?这是可取的吗?

解决方法:

感谢Sven Arduwie的代码(pointed out by Pekka)和一些修改,我已经构建了一个(希望)更好的实现:

/**

* This function is to replace PHP's extremely buggy realpath().

* @param string The original path, can be relative etc.

* @return string The resolved path, it might not exist.

*/

function truepath($path){

// whether $path is unix or not

$unipath=strlen($path)==0 || $path{0}!='/';

// attempts to detect if path is relative in which case, add cwd

if(strpos($path,':')===false && $unipath)

$path=getcwd().DIRECTORY_SEPARATOR.$path;

// resolve path parts (single dot, double dot and double delimiters)

$path = str_replace(array('/', '\'), DIRECTORY_SEPARATOR, $path);

$parts = array_filter(explode(DIRECTORY_SEPARATOR, $path), 'strlen');

$absolutes = array();

foreach ($parts as $part) {

if ('.' == $part) continue;

if ('..' == $part) {

array_pop($absolutes);

} else {

$absolutes[] = $part;

}

}

$path=implode(DIRECTORY_SEPARATOR, $absolutes);

// resolve any symlinks

if(file_exists($path) && linkinfo($path)>0)$path=readlink($path);

// put initial separator that could have been lost

$path=!$unipath ? '/'.$path : $path;

return $path;

}

注意:与PHP的realpath不同,此函数在出错时不返回false;它返回一条路径,尽可能地解决这些怪癖.

注2:显然有些人无法正常阅读. Truepath()不适用于包括UNC和URL在内的网络资源.它仅适用于本地文件系统.

标签:php,path,realpath

来源: https://codeday.me/bug/20190926/1821606.html

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。