在 PHP 中创建绝对路径的最佳方法?(见里面列出的3种方法) [英] Best method for creating absolute path in PHP? (See 3 methods listed inside)

查看:34
本文介绍了在 PHP 中创建绝对路径的最佳方法?(见里面列出的3种方法)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以毫无问题地创建路径,但我想知道这 3 种方法中的哪一种是最可靠和最可靠的,并且可以在最多的服务器上运行.

I can create paths with no problem, but I want to know which of these 3 methods is the most rock solid and reliable and will work on the most servers.

现在我在我的脚本中使用方法 1,一些用户遇到了路径问题.我只想要一种适用于任何版本的 php 和几乎所有服务器配置的方法.

Right now I am using method 1 in my script and some users are having path issues. I just want the method that will work on any version of php and almost any server config.

1.  <?php echo $_SERVER['DOCUMENT_ROOT']; ?>

2.  <?php echo getcwd(); ?>

3.  <?php echo dirname(__FILE__); ?>

非常感谢您提供有关这方面的任何专业知识!

Thank you so much for any expertise you can provide about this!

推荐答案

dirname(__FILE__) 将始终有效,无论平台或网络服务器如何.DOCUMENT_ROOT 在不同服务器配置(Apache 与 IIS、Lighttpd 与 nginex)之间的工作方式可能不同.cwd 显示选定的工作目录,它可能正确也可能不正确(您可以在脚本中更改它).所以我建议 dirname(__FILE__)

dirname(__FILE__) will always work, regardless of platform or webserver. DOCUMENT_ROOT may work differently between server configurations (Apache vs IIS vs Lighttpd vs nginex). cwd shows the selected working directory which may or may not be correct (you can change it in the script). So I'd suggest dirname(__FILE__)

这篇关于在 PHP 中创建绝对路径的最佳方法?(见里面列出的3种方法)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆