This should be a simple question, but I just can't recall the relevant API. A search on google with the term "" doesn't yield any results . So I guess I am doing both myself and the programming community a service by asking this question. this is now the top entry returned by Google and DDG!
PHP中如何将目录和文件名组合成完整的文件路径?假设目录名称是“D:\setup program
”,文件名是“mj.txt
“。该方法应该返回我,在 Windows 上”D:\setup program\mj.txt
当然,该方法应该返回Linux或其他操作系统中的正确文件路径。
.Net中的相关函数是路径组合 http://msdn.microsoft.com/en-us/library/system.io.path.combine.aspx,但是在 PHP 中,我不记得了,尽管我以前肯定见过它。
$filepath = $path . DIRECTORY_SEPARATOR . $file;
尽管在较新版本的 PHP 中,斜杠的走向并不重要,因此始终使用正斜杠就可以了。
您可以使用以下命令获得正确的绝对路径realpath() http://php.net/realpath,这也将删除诸如额外不必要的斜杠之类的内容并解析诸如../
。如果路径无效,它将返回 false。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)