当引用 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/