php 相对路径:PHP的相对路径问题
在PHP中require,include一个文件时,大都是用相对路径,是个很头疼的问题。
例如:
├文件夹
│ │
│ └1.php
├文件夹
│ │
│ └2.php
└index.php
问题:在1.php中通过include(“../B/2.php”)来引入B目录下的2.php文件;
在index.php中通过include(“A/1.php”)来引入A目录下的1.php文件;
运行出来当然会出现问题,找不到../B/2.php文件。
记住一个原则,所有的包含语句最后都转换为以包含文件路径为基准的包含路径建议使用一个基本路径来作为所有的路径参考,例如 __FILE__或者 $_SERVER['DOCUMENT_ROOT']
__FILE__:(适合各种情况)
在config文件里配置站点根目录
define( "WEB_ROOT ", dirname(__FILE__) );
config文件放到网站的根目录里,
其它文件包含配置文件,
包含其它文件时根据WEB_ROOT来定位即可。
require_once( WEB_ROOT . "/a.php " );
$_SERVER['DOCUMENT_ROOT'],适合非虚拟主机(Alias)情况
<ol class="dp-c"> <li class="alt"><span><span><?php </span></span></span></li> <li><span><span class="keyword">if</span><span> (!defined(</span><span class="string">"WETSITE_BASE_DIR"</span><span>)) </span></span></li> <li class="alt"><span>define(<span class="string">"WETSITE_BASE_DIR"</span><span>, </span><span class="vars">$_SERVER</span><span>[</span><span class="string">'DOCUMENT_ROOT'</span><span>].</span><span class="string">'/Clare/'</span><span>); </span></span></li> <li><span><span class="keyword">require_once</span><span>(WETSITE_BASE_DIR.</span><span class="string">'includes/global.inc.php'</span><span>); </span></span></li> <li class="alt"><span>?> </span></li> </ol>

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



In this chapter, we will understand the Environment Variables, General Configuration, Database Configuration and Email Configuration in CakePHP.

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

Working with database in CakePHP is very easy. We will understand the CRUD (Create, Read, Update, Delete) operations in this chapter.

To work on file upload we are going to use the form helper. Here, is an example for file upload.

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

In this chapter, we are going to learn the following topics related to routing ?

Validator can be created by adding the following two lines in the controller.
