In-depth analysis of the usage of constant DIRECTORY_SEPARATOR in php, separator_PHP tutorial

WBOY
Release: 2016-07-13 10:14:07
Original
868 people have browsed it

In-depth analysis of the usage of constant DIRECTORY_SEPARATOR in php, separator

The example in this article describes the usage of the constant DIRECTORY_SEPARATOR in php. Share it with everyone for your reference. The details are as follows:

What does DIRECTORY_SEPARATOR mean in php? When is it most reasonable to use DIRECTORY_SEPARATOR? Let me introduce to you the php DIRECTORY_SEPARATOR constant.

We know that DIRECTORY_SEPARATOR is a PHP constant, representing a backslash, because the backslashes of Windows and Linux systems are different. In Windows, both slash (/) and backslash () can be used as directory separators. On Linux, the path separator is "/".

So when should DIRECTORY_SEPARATOR be used to represent backslash in PHP code?
For example, this sentence:

Copy code The code is as follows:
require_once dirname(__FILE__).DIRECTORY_SEPARATOR . './../../../wp-config. php';
require_once dirname(__FILE__).DIRECTORY_SEPARATOR.'inc/options.php';

Why does the first part of these two PHP codes use DIRECTORY_SEPARATOR, but the latter part uses "/"
As we all know, under Windows it is and/, while under LINUX it is /

The program runs normally locally. After uploading it to the server, I found that the image was not displayed. The image link was an absolute path var/, and it contained "But I remember that this path has been processed into a relative path"

Copy code The code is as follows:
$path = dirname(dirname(dirname(__FILE__))).'\';
$search = array( $path, '\');
$replace = array( '', '/');
return str_replace($search,$replace,$this->tempfolder).$this->filename_prefix.$public.'.jpg';

As you can see, the first line of code does not work when used on LINUX "\".

The solution is to use the PHP predefined constant DIRECTORY_SEPARATOR to replace path separators such as '' and '/'. I used to think that since Windows and LINUX both support /, then use /. This is correct. But as can be seen from this example, it is more dangerous when path strings are to be processed, so it is best to use DIRECTORY_SEPARATOR

I hope this article will be helpful to everyone’s PHP programming design.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/911906.htmlTechArticleIn-depth analysis of the usage of the constant DIRECTORY_SEPARATOR in php, separator This article explains the usage of the constant DIRECTORY_SEPARATOR in php. Share it with everyone for your reference. The details are as follows: DIRECTOR...
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template