A problem with the PHP access root directory?

Source: Internet
Author: User
I use the system is CentOS, the site root directory is/var/www/html, the root directory is put head.inc.php, when I in index.php require (' head.inc.php ') Everything is normal, require ('. Head.inc.php ') is also normal when require ('/head.inc.php ') is found failed to open stream:no such file or directory error

The front slash represents the root directory, don't I set it wrong? Apache's DocumentRoot and directory are all set to/var/www/html,www,html and files are set to Rwxr-xr-x, are there any other settings that need to be set? Thank you!


Reply to discussion (solution)

Require ('/head.inc.php ') refers to the/var/www/html/head.inc.php that PHP considers head.inc.php to be a directory under the HTML path, not a PHP file.

Or you can write this: require (dirname (__file__). /head.inc.php ')

Require is using the path of the file system instead of the Web

Thanks Changjay and xuzuning, but DirName (__file__) Gets the path to the current file? I want to get the root directory, such as a subdirectory file/var/www/html/report/index.php also reqire root directory head.inc.php, if used DirName (__file__), it becomes require ' /var/www/html/report/head.inc.php '

The moderator refers to the file system path, I think it may be the reason, I tried to change to/var/www/html/head.inc.php

is not you this site node root directory is not configured well!

Moderator, I tried to change the file system directory, you can

ZMZKKK, I've set Apache's DocumentRoot and directory to/var/www/html, do you have anything else to change?

Online say with/On the root directory, as if include also by the file system directory, this problem Google for a half a day and few people mention

What better solution is there for everyone? Evening Knot Stickers

Yes/indicates root directory

Since you are doing the website, then you have to face two systems, that is, there are two root
Web site files are stored in the operating system's file system,/var/www/html,/is the root of the file system
Browser access is http://localhost/index.php,/is the root of the web system

The root of the web system is specified by you in the Web server, such as
DocumentRoot =/var/www/html

In fact, PHP has given a solution: Include_path

Thanks Changjay and xuzuning, but DirName (__file__) Gets the path to the current file? I want to get the root directory, such as a subdirectory file/var/www/html/report/index.php also reqire root directory head.inc.php, if used DirName (__file__), it becomes require ' /var/www/html/report/head.inc.php '

The moderator refers to the file system path, I think it may be the reason, I tried to change to/var/www/html/head.inc.php



If you have a configuration file, you can assign the site root to a variable in the configuration file, and then use it where you want it.

Thank you moderator, but I changed the/etc/php.ini include_path= ".:/ Var/www/html ", or it will appear:
Fatal Error:require () [function.require]: Failed opening required '/head.inc.php ' (include_path= '.:/ var/www/html ') in XXX

I restarted Linux and confirmed that all the files under Var,www,html and HTML were 755, and it turned off SELinux (getenforce=permissive), but it was still a mistake. Changed to what you said require ('/var/www/html/head.inc.php ') can, I'll check

Novolee, I've also thought of a global variable, but the absolute path will still leak into the PHP code.

Found another variable $_server[' document_root ', I used this variable instead/again

is directly used/why not yet

Found another variable $_server[' document_root ', I used this variable instead/again

is directly used/why not yet



Accept?? And the big thing: Use your earliest way./unless your plan is to be under the command line? Lu?? All.

Found another variable $_server[' document_root ', I used this variable instead/again

is directly used/why not yet


Isn't there a response from the front? The root directory of the operating system file system, not the root directory of the site, is used directly/.

Phpnewnew, my web page has several layers, but the header.inc.php,css style sheets are all in the root directory, so it's much easier to load them directly from the root directory./can only read to the current directory

Novolee, yes, but PHP must be written/var/www/html/head.inc.php, a little worried that such a path will not be accidentally displayed in the Web page, so still use ' document_root

Thank you, knot stickers.

  • 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.