PHP中路径问题的解决方案
PHP中路径问题的解决方案
引言:
关于PERL与PHP中的包含路径一直是一个比较难解的问题,主要是与操作系统和WEB服务器有关,不可能非常智能化的解决这个路径问题。相对于PERL,PHP的路径好得多,解决起来也容易得多,因为PHP的相对路径在PHP程序中的任何场合都可以使用,而不象PERL在某些语句中必须用绝对路径而导致移植的极其复杂。
基于此,在PHP中,我设计了一个绝对稳妥的解决方案,如下所述。
原则:
使用相对路径,但在相对路径中用绝对路径(有点绕,待会儿详解),一是可以保证可移植性,二是可以方便地修改,三是公式化且结构清晰明了,易于扩展。
步骤详解:
1、先确定好一个程序的根目录,注意是文件系统下的,不是WEB服务器下的虚拟目录,不过一般情况下该目录下的子目录的相对路径与URL下该目录的虚拟子目录是相同的。
2、在定义好的程序根目录下的每个子目录下(其实不一定是每个,根据需要)建立一个settings.php,里面定义一个变量或是常数(常数比较好,因为作用域比较大),如APPROOT,但这个APPROOT却不是绝对路径,而是该目录相对于你指定的程序根目录的相对路径。
3、在此目录下的所有程序入口文件(也即第一个包含其它文件的文件,或是允许直接在浏览器中浏览的文件)中第一句写上require_once('settings.php');,但要注意,所有被包含文件最好不要加此句——其实加上也可以,因为你可以在settings.php中写上 if(!defined(APPROOT)) define(APPROOT, '../..');这类的语句以防重定义。
4、如果你要包含其它文件,无论是直接还是间接地包含,都可以写成 include(APPROOT.$path);,这里$path为被包含文件相对于你所指定的程序根目录的绝对路径。
原理:
定下的程序根目录是相对路径,但具体的目录位置是相对于那个根目录的绝对路径,两者组合起来就是具体文件相对于程序根目录的相对路径了。例如目录c:\wwwroot\app为你指定的程序根目录,然后有这么两个文件c:\wwwroot\app\a\index.php和c:\wwwroot\app\b\inc.php。对子目录a来说,APPROOT是'..',而对程序根目录来说,inc.php的绝对路径是$path='/b/inc.php',两者组合为'../b/inc.php'。如果要在index.php中包含inc.php就要写成include('../b/inc.php');,而这个路径不就正好是刚才组合而成的APPROOT.$path吗?
结论:
经过以上处理,各个路径绝对整齐划一,唯一罗嗦一点的就是每个目录下要定义一下这个APPROOT,但每个目录下只需在本目录的settings.php中定义一次就足够了。如果你整个程序只有一个入口文件,如index.php,而其它文件全部都是直接或是间接地被包含进这个唯一的入口文件的话,就只需在index.php的所在目录下的settings.php中定义一次就OK了。如果有朋友做过Delphi的工程并对工程文件研究过的话,就会发现我刚才说的一个程序只有一个主入口文件的情况与Delphi的工程十分相似,因为Delphi除了一个主程序文件(dpr文件),其余的全部是单元文件或是资源文件,都不能独立执行。在PHP中,如果这种情况出现,只需定义一次APPROOT,并在主程序文件中第一句话写成require_once('settings.php');,而以后所有的包含全都可以用include(APPROOT.$path);,就保证不会有任何问题,除非你不会写这个“包含文件相对于程序根目录的绝对路径”$path。
这个方法我用了不止一次,收效很好。另外还可以参考JSP的WEB-INFO中路径的定义方式。
我这个是以不变应万变的公式化的方案,如果有朋友有更好的方案,欢迎提出讨论!如有不明白的也欢迎提出。

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

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

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

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

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.
