PHP dirname in Linux (

Source: Internet
Author: User
Tags php code

In PHP, the dirname () function returns the directory portion of the path, __file__ the full path and file name of the currently running file. If used in the included file, the included file name is returned. This is a magic variable (predefined constants), in Windows there is no problem but the path in Linux problems, let's take a look at the path problem resolution.

In the recent development of WordPress template features, found that the use of include ("filename") in the form of other PHP code snippets will appear when the path error. The server environment has always been IIS, and no similar bug has occurred, but a path call error occurred after switching to a Linux server. After searching the Web, you can use DirName (__file__) To troubleshoot path errors when the Linux server contains files under the call.

In fact, the main problem is from the Linxu server root directory problems, when the Linux server contains a file, its path is/home/web/. If you use the format of include ("/filename") directly, the file path that is actually contained is the/home/web/file name.

The complete use method is include DirName (__file__). '/page.php '

This code references a file that needs to be in the same directory as the code file, and if you need to call the previous level directory, you can use DirName (dirname (__file__)) to invoke the file on the previous level of the directory.

If you encounter a problem with a class after the site has changed the server, check to see if the site has a reference path error. It is recommended that you use the DirName (__file__) method to refer to files when writing PHP inclusions to address the most problematic references.

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.