PHP Directory_separator Constants Usage Explained

Source: Internet
Author: User
Directory_separator is a command that displays the system delimiter, Directory_separator is the internal constant of PHP and is not required to be used for any definition or inclusion.
As we all know, under the Windows path separator is (of course, also can work on some systems), on Linux on the path delimiter is/, which leads to a problem, such as the development machine is windows, there is a picture upload program, The upload file specified on the debug machine is saved in the directory: define (' ROOT ', dirname (file). " Upload "), local debugging is normal, but uploaded to the Linux server will be found to be error.
This problem is out of the file delimiter, Windows habitual use as a file delimiter, but on the Linux people do not know this logo, people only know/, so it is necessary to introduce the following PHP built-in variables: Directory_separator.
The above notation can be rewritten as the following error-free notation:
Define (' ROOT ', DirName (FILE). Directory_separator. " Upload ");
This ensures that there are no errors.
For example Discuz is written in this way: define (' S_root ', DirName (FILE). Directory_separator);
Back to the problem itself, Directory_separator is a PHP built-in command that returns a path delimiter associated with the operating system, returned on Windows, and returned on Linux or UNIX, which is the difference, This is usually used when you define a file path or upload a directory to save it.

Require_once dirname (FILE). Directory_separator. './.. /.. /.. /wp-config.php '; require_once dirname (FILE). Directory_separator. ' inc/options.php ';

These two PHP code, why the previous section with Directory_separator, and the latter part of the "/"?
As we all know, under Windows is \ and/, while Linux is/

In the program local operation is normal, uploaded to the server, found that the picture is not displayed, the image link is the absolute path var/, and with a \ But I remember this path has been processed into a relative path

$path    = dirname (dirname (dirname (FILE)). ' \ \ '; $search = Array ($path, ' \ \ '), $replace = Array (', '/'), return Str_replace ($search, $replace, $this->tempfolder). $this->filename_prefix. $public. JPG ';

The first line of code is used in Linux "\ \".

The solution is to use PHP pre-defined constant directory_separator instead of ' \ ', '/' Such a path delimiter, previously felt that since windows,linux are supported/, it is used/won, this is true. But from this example it can be seen that when the path string to be processed, it is more dangerous, so it is better to use Directory_separator bar

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.