Php constant DIRECTORY_SEPARATOR usage in-depth analysis, separator

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.