Php constant DIRECTORY_SEPARATOR usage in-depth analysis, separator
This example describes the usage of the constant DIRECTORY_SEPARATOR in php. Share it with you for your reference. The details are as follows:
What does DIRECTORY_SEPARATOR mean in php? When is it most reasonable to use DIRECTORY_SEPARATOR? The following describes the php DIRECTORY_SEPARATOR constant.
We know that DIRECTORY_SEPARATOR is a PHP constant that represents a backslash, because the backslash in windows and linux systems is different. In Windows, both slash (/) and backslash (\) can be used as Directory separators. on linux, the path Delimiter is "/".
So when will DIRECTORY_SEPARATOR be used in PHP code to represent a backslash?
For example:
Copy codeThe Code is as follows: require_once dirname (_ FILE _). DIRECTORY_SEPARATOR. './.../wp-config.php ';
Require_once dirname (_ FILE _). DIRECTORY_SEPARATOR. 'inc/options. php ';
The above two PHP codes use DIRECTORY_SEPARATOR, while the latter uses "/".
As we all know, What Is \ and/in windows, while what is in LINUX is/
The program runs normally locally. After being uploaded to the server, the image is not displayed. The image link is the absolute path var /, it contains \, but I remember that this path has been processed as a relative path.
Copy codeThe Code is as follows: $ path = dirname (_ FILE __))).'\\';
$ Search = array ($ path ,'\\');
$ Replace = array ('','/');
Return str_replace ($ search, $ replace, $ this-> tempfolder). $ this-> filename_prefix.#public.'.jpg ';
You can also see that the first line of code is used in LINUX.
The solution is to use the PHP pre-defined constant DIRECTORY_SEPARATOR to replace path delimiters like '\' and '/'. In the past, I thought that since windows and LINUX support/, they all use, this is correct. However, from this example, we can see that it is dangerous to process the path string, so we 'd better use DIRECTORY_SEPARATOR.
I hope this article will help you with PHP programming.