Since I change the tree, absolute and relative path don#39;t work(由于我更改了树,因此绝对和相对路径不起作用)
问题描述
我尝试使用绝对路径来包含我的文件:
I tried to use the absolute path to include my files :
我有 4 个文件(我的本地主机上还有其他文件,但奇怪的是包含效果很好):
I have 4 files (I have other file on my localhost but oddly the inclusion works well) :
header.php (C:wampwwwMySitelayoutheader.php)
<?php session_start ();
require_once '/pdo.php';
....
pdo.php (C:wampwwwMySitepdo.php)
<?php
require_once '/class/User.php';
require_once '/class/Order.php';
....
forms/login.php (C:wampwwwMySiteformslogin.php)
<?php
session_start ();
include '/pdo.php';
....
login.php (C:wampwwwMySitelogin.php)
<?php
$title = 'Connexion';
include ("/layout/header.php");
...
所以它看起来像:
Root
- forms
- login.php
-layout
- header.php
- pdo.php
- login.php
我有这个错误:
( ! ) 警告:include(/pdo.php):无法打开流:第 3 行的 C:wampwwwMySiteformslogin.php 中没有这样的文件或目录调用栈
( ! ) Warning: include(/pdo.php): failed to open stream: No such file or directory in C:wampwwwMySiteformslogin.php on line 3 Call Stack
( ! ) 警告:include():无法在 C:wampwwwMySiteformslogin 中打开包含 (include_path='.;C:phppear') 的/pdo.php".第 3 行的 php
( ! ) Warning: include(): Failed opening '/pdo.php' for inclusion (include_path='.;C:phppear') in C:wampwwwMySiteformslogin.php on line 3
( ! ) 致命错误:在第 12 行的 C:wampwwwMySiteformslogin.php 中找不到用户"类
( ! ) Fatal error: Class 'User' not found in C:wampwwwMySiteformslogin.php on line 12
但是我在很多文件上都有这个问题,因为我想更改文件和文件夹的树状结构(树)..
But I have this problem on a lot of files since I wanted to change the arboresence (tree) of files and folder ..
我该如何解决这个问题?以及我将来如何避免这个问题?
How I can solve this problem ? and how I can do to avoid this problem in the future ?
谢谢
推荐答案
我尝试使用绝对路径来包含我的文件:
I tried to use the absolute path to include my files :
header.php (C:wampwwwMySitelayoutheader.php)
header.php (C:wampwwwMySitelayoutheader.php)
<?php session_start ();
require_once '/pdo.php';
PHP 代码在服务器上执行.此上下文中的绝对路径是指文件系统绝对路径,而不是 Web 主机路径.由于您使用的是 Windows,/pdo.php 实际上意味着 C:/pdo.php 而不是 C:wampwwwMySitepdo.php 就像你想的那样.
The PHP code is executed on the server. An absolute path in this context means a file-system absolute path, not a web host path. Since you are on Windows, /pdo.php in fact means C:/pdo.php and not C:wampwwwMySitepdo.php as it seems you think.
在 PHP 中处理路径的最佳方法,关于 include 和 require 是使用 __FILE__ 和 __DIR__ 魔法常量和 dirname() PHP 函数,用于从文件的相对位置开始构建文件的(文件系统)绝对路径.
The best way to work with paths in PHP, regarding include and require is to use the __FILE__ and __DIR__ magic constants and the dirname() PHP function to build the (file-system) absolute paths of files starting from their relative locations.
您的代码变为:
header.php (C:wampwwwMySitelayoutheader.php):
<?php
session_start ();
// 'pdo.php' is one level up
require_once dirname(__DIR__).'/pdo.php';
....
pdo.php (C:wampwwwMySitepdo.php)
<?php
// User.php is inside the 'class' subdirectory
require_once __DIR__.'/class/User.php';
require_once __DIR__.'/class/Order.php';
....
dir1/dir2/dir3/file.php (C:wampwwwMySitedir1dir2dir3file.php)
<?php
// 'header.php' is in the 'layout' subdirectory of the grand-grand parent directory
include dirname(dirname(dirname(__DIR__))).'/layout/header.php';
备注
此处提供的解决方案使代码独立于其在文件系统中的实际位置.您可以将整个项目(C:wampwwwMySite 中的所有内容)移动到不同的目录或不同的计算机上,并且无需更改即可正常工作.更重要的是,如果您使用正斜杠 (/) 作为目录名称分隔符,它适用于 Windows、macOS 或任何 Linux 版本.
Remark
The solution presented here makes the code independent of its actual location in the file system. You can move the entire project (everything in C:wampwwwMySite) in a different directory or on a different computer and it will work without changes. Even more, if you use forward slashes (/) as directory names separators it works on Windows, macOS or any Linux flavor.
这篇关于由于我更改了树,因此绝对和相对路径不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:由于我更改了树,因此绝对和相对路径不起作用
基础教程推荐
- php中的foreach复选框POST 2021-01-01
- Web 服务器如何处理请求? 2021-01-01
- Yii2 - 在运行时设置邮件传输参数 2022-01-01
- php 7.4 在写入变量中的 Twig 问题 2022-01-01
- 将变量从树枝传递给 js 2022-01-01
- 使用 scandir() 在目录中查找文件夹 (PHP) 2022-01-01
- 如何在数学上评估像“2-1"这样的字符串?产生“1"? 2022-01-01
- php中的PDF导出 2022-01-01
- PHPUnit 的 Selenium 2 文档到底在哪里? 2022-01-01
- 主题化 Drupal 7 的 Ubercart “/cart"页 2021-01-01
