When referring to directories in PHP such as in the code below.

index.php

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

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

        ...

    }

    closedir($handle);
}

The setup we can assume is below. The following denotes a [directory], all code is in the index.php file and the imeages/files we wish to iterate through are in the [images] directory.

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

Specifically the opendir('/path/to/images/directory')) { line, what are the best practices to referencing that directory?

Should there be a trailing / at the end as it is a directory or is the unnecessary? Should it be relative? Absoulte? Could we use SERVER_VARIABLES instead?

有帮助吗?

解决方案

For realtive / absolute convention i suggest relative, but maybe is just me. It allows your code to be relocated easier.

Then , curiosly , i checked php source code and opendir is wrapped around various c functions and one of those is

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                 }

So basically it loops in your path jumping from one DEFAULT_DIR_SEPARATOR to another. So i guess, less you have the better is.

Also reading your code, but maybe is just my taste: USing $dir."/".$filename looks better than $dir.$filename

Anyway no real world difference i guess.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top