Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/262.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 有/没有'/';在最后定义目录时?_Php_Coding Style_Directory_Standards_Opendir - Fatal编程技术网

Php 有/没有'/';在最后定义目录时?

Php 有/没有'/';在最后定义目录时?,php,coding-style,directory,standards,opendir,Php,Coding Style,Directory,Standards,Opendir,在PHP中引用目录时,如下面的代码中 index.php if ($handle = opendir('/path/to/images/directory')) { while (false !== ($fileName = readdir($handle))) { ... } closedir($handle); } 我们可以假设的设置如下。下面表示一个[目录],所有代码都在index.php文件中,我们希望迭代的images/文件在[图像]目

在PHP中引用目录时,如下面的代码中

index.php

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

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

        ...

    }

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

-[css]
-[js]
-[inc]
-[images]
-index.php
特别是
opendir('/path/to/images/directory'){
line,引用该目录的最佳实践是什么?

末尾是否应该有一个尾随的
/
,因为它是一个目录,还是不需要? 应该是相对的吗?绝对的?
我们可以用
服务器变量来代替吗?

对于实际/绝对约定,我建议是相对的,但可能就是我。 它使您的代码更容易重新定位

然后,奇怪的是,我检查了php源代码,opendir被包装在各种c函数中,其中之一就是

php\u check\u open\u 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                 }
所以基本上它在你的路径中循环,从一个默认的分隔符跳到另一个。 所以我想,你拥有的越少越好

也在阅读您的代码,但可能只是我的口味: 使用$dir.“/”$filename看起来比$dir.$filename更好


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

我的意思是一般而言,不仅仅是与我的代码有关。我的代码是要给出一个例子。我想问的是,在定义目录时,我必须始终在末尾有一个
/
吗?是的,我的意思是一般。最后一个/实际上没有使用,差异是最小的,不值得注意。对于绝对或相对而言例如,我建议相对的,以防有人在其他服务器/位置上移动他的代码。