php - 定义目录时末尾有/没有 '/' 有什么区别?

标签 php coding-style directory standards opendir

当引用 PHP 中的目录时,例如下面的代码。

index.php

if ($handle = opendir('/path/to/images/directory')) {

    while (false !== ($fileName = readdir($handle))) {

        ...

    }

    closedir($handle);
}

我们可以假设的设置如下。下面表示一个[directory],所有代码都在index.php文件中,我们希望迭代的图像/文件在[images]中 目录。

-[css]
-[js]
-[inc]
-[images]
-index.php

特别是 opendir('/path/to/images/directory')) { 行,引用该目录的最佳实践是什么?

最后是否应该有一个尾随 / 因为它是一个目录,或者是不必要的? 应该是相对的吗?绝对吗? 我们可以使用 SERVER_VARIABLES 来代替吗?

最佳答案

对于相对/绝对约定,我建议相对,但也许只是我。 它使您的代码可以更轻松地重新定位。

然后,好奇的是,我检查了 php 源代码,opendir 包含了各种 C 函数,其中之一是

php_check_open_basedir

 313                 while (ptr && *ptr) {
 314                         end = strchr(ptr, DEFAULT_DIR_SEPARATOR);
 315                         if (end != NULL) {
 316                                 *end = '\0';
 317                                 end++;
 318                         }
 319 
 320                         if (php_check_specific_open_basedir(ptr, path TSRMLS_CC) == 0) {
 321                                 efree(pathbuf);
 322                                 return 0;
 323                         }
 324 
 325                         ptr = end;
 326                 }

所以基本上它会在你的路径中循环,从一个 DEFAULT_DIR_SEPARATOR 跳到另一个。 所以我想,拥有的越少越好。

也在阅读你的代码,但也许只是我的口味: 使用 $dir."/".$filename 看起来比 $dir.$filename 更好

无论如何,我猜没有现实世界的差异。

关于php - 定义目录时末尾有/没有 '/' 有什么区别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19111242/

相关文章:

c# - PHP 和 C# HMAC SHA256

c - 如何打开具有相对路径的C文件?

PHP、Yii - 未知属性异常

iphone - iphone设置透明状态栏的方法

c++ - 检测 C++ 代码样式问题的工具?

python - 我应该让我的 python 代码不那么简单以提高可读性吗?

python - 每次将文件下载到特定文件夹时都会触发 python 脚本

c#保存文件目录

javascript - 如何在一页中放置多个数据表?

php - mysql查询中使用目录函数