English 中文(简体)
Proper way to set PHP include path for *Nix and Windows
原标题:

Is this the proper way to define an include path for both *nix and Windows?

define(  INCPATH , realpath( dirname( __FILE__ ) ) .  /  );

Note the trailing forward-slash I included above. Is the forward-slash for includes/requires the same for both OS s, as well?

EDIT (UPDATED WITH ANSWER):

From what I can gather, my code below is the proper way to universally define an include/require path for both *nix and Windows OS s. Feel free to correct anything in the comments below.

The thing that confused me were the many examples I saw showing replacement of back-slashes () into forward-slashes(/). Based on some of the answers below, this is unnecessary.

So the final correct code (for the purist) is:

define(  INCPATH , realpath( dirname( __FILE__ ) ) . DIRECTORY_SEPARATOR );

That code produces the following results:

*nix: /path/to/the/file/

Windows: C:Path To hefile

A brief explanation, working our way from the inside (__FILE__) out (realpath()):

FILE The full path and filename of the file. Always contains an absolute path with symlinks resolved.

dirname() The returned string is path with any trailing /component removed. Responsible for removing the filename.

realpath() Returns the canonicalized (normalized/standardized) absolute pathname on success. The resulting path will have no symbolic link, /./ or /../ components. I assume this is included for thoroughness because __FILE__ already resolves symlinks. Or maybe it s included to resolve relative paths? Either way, it seems to solidify our goal.

最佳回答

Forward slashes will work for both OS s, and it s the way to go.

I couldn t find an absolute reference to this, but it s indicated in several places in the PHP manual, like here and here. And, it works for me, a Windows & Linux user.

Lastly, you may end up specifying mixed-paths on Windows, like c:\apache\htdocs\myapp/index.php, and that all works fine.

问题回答

Alternatively you can use PHP s predefined constant DIRECTORY_SEPARATOR, which will give you the OS-specific directory delimiter. See http://www.php.net/manual/en/dir.constants.php also.

To many people s surprise, / works fine on Windows—and MSDOS. Within pathnames, it works even on OpenVMS.

However, if you are doing something within PHP for paths, an array would be a more convenient structure than a string.

$MYPATH = array ( . ,  /usr/lib/ ,  /usr/share/lib );




相关问题
Signed executables under Linux

For security reasons, it is desirable to check the integrity of code before execution, avoiding tampered software by an attacker. So, my question is How to sign executable code and run only trusted ...

encoding of file shell script

How can I check the file encoding in a shell script? I need to know if a file is encoded in utf-8 or iso-8859-1. Thanks

How to write a Remote DataModule to run on a linux server?

i would like to know if there are any solution to do this. Does anyone? The big picture: I want to access data over the web, using my delphi thin clients. But i´would like to keep my server/service ...

How can I use exit codes to run shell scripts sequentially?

Since cruise control is full of bugs that have wasted my entire week, I have decided the existing shell scripts I have are simpler and thus better. Here is what I have so far svn update /var/www/...

Good, free, easy-to-use C graphics libraries? [closed]

I was wondering if there were any good free graphics libraries for C that are easy to use? It s for plotting 2d and 3d graphs and then saving to a file. It s on a Linux system and there s no gnuplot ...

热门标签