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:
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"
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.