- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
显然,realpath
有很多问题。在 PHP 5.3.1 中,它会导致随机崩溃。在 5.3.0 及更低版本中,realpath
随机失败并返回 false(当然是针对相同的字符串),而且它总是在 realpath
上失败 - 对相同的字符串进行两次/更多次(当然,它是第一次工作)。
此外,它在早期的 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 - 替换 PHP 的 realpath(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4049856/
出于性能原因,我们应该只使用 realpath()而不是 realpath() + file_exists()在检查文件或目录是否存在时 ?? 案例 A:: if(realpath(__DIR__."
做这样的事情是不是一种感觉 $dir = realpath(dirname(__FILE__)); // or for php 5.3+: $dir = realpath(__DIR__) 或者
调用 Unix realpath() 函数的传统方法是 realpath(pathname, buf) 其中 buf 是用户提供的PATH_MAX 字节空间的缓冲区。这是有问题的,因为 PATH_MA
这个问题在这里已经有了答案: Unexpected behavior in printing a char array in C (1 个回答) 关闭 8 年前。 这是我的功能,它在给定目录中查找常
PHP 的 realpath() 在 manual 中有详细描述,我只是想了解它在哪些场景下有用。因为看起来我的代码可以很好地处理 ../../path 这样的路径,所以我不确定 realpath()
我使用 MediaWiki,但出现错误。我在文件中找到了它。有一行代码: $IP = realpath( '.' ); 但是 $IP 返回一个空字符串,所以系统中文件的路径不起作用。 (例如,如果 $
我想检查一个路径是否是另一个路径的子路径,所以我调用 realpath(file_path, NULL) 并将结果与 strncmp 进行比较。 我写这个主题的原因是:realpath 设置err
我在使用 realpath 函数时遇到了一个奇怪的问题。该函数在给定一个作为程序参数接收的字符串时工作,但在给定我在源代码中定义的字符串时失败。这是一个简单的程序: #include #includ
我正在开发一个程序,它使用 realpath() 来获取文件的绝对路径。不幸的是,这个函数需要一个字符串缓冲区,这个缓冲区应该很大,以至于它足够大,当这个应用程序必须跨多个平台运行时,这是不安全的。是
给定 现代 Linux/UNIX/OSX(带 realpath) bash 4+(甚至在 OSX 上) 是 "$PWD" == "$(realpath .)" 总是正确的? 最佳答案 很容易测试情况并
我的 Windows 服务器上有一个符号链接(symbolic link),它是这样制作的: F:\>mkdir link-target F:\>mklink /D link f:\link-targ
当我编译以下代码时: #define _POSIX_C_SOURCE 200112L #define _ISOC99_SOURCE #define __EXTENSIONS__ #include #
我在使用 File.realpath() 时遇到问题,留下的字符串似乎没有被垃圾收集。在我看来,这像是内存泄漏,但我无法想象这样的事情对于核心库方法来说真的是真的。 考虑以下代码: def strin
我已经养成了使用以下命令在我的编辑器中快速打开相关文件进行代码审查的习惯。 git diff --name-only master... | xargs realpath | someEditor 但
我的一个扩展要求我将 realpath_cache_size 设置为至少 512k。那么我是否必须将它设置在 .htaccess 中,例如: realpath_cache_size = 512k 以及
我已经读过有关 realpath() 的内容,但是是否有一个函数可以传递基本目录和文件名,可以在不解析符号链接(symbolic link)或检查文件是否实际存在的情况下给出以下结果?或者我是否必须使
我已经阅读过有关 realpath() 的内容,但是是否有一个函数可以传递一个基本目录和一个文件名,从而在不解析符号链接(symbolic link)或检查文件是否实际存在的情况下给出以下结果?还是我
我想获取当前执行脚本的路径,我用过 os.path.realpath(__file__),但是,它会返回类似 D:\My Stuff\Python\my_script.py 的字符串,而没有适当的反斜
显然,realpath 有很多问题。在 PHP 5.3.1 中,它会导致随机崩溃。在 5.3.0 及更低版本中,realpath 随机失败并返回 false(当然是针对相同的字符串),而且它总是在 r
我有以下代码: os.chdir(os.path.dirname(os.path.realpath(__file__)) + "/../test") path.append(os.getcwd())
我是一名优秀的程序员,十分优秀!