包括、要求和相对路径 [英] include, require and relative paths

查看:52
本文介绍了包括、要求和相对路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不知道为什么我总是遇到很多问题.无论如何,这是我想要的文件的路径

I'm not sure why I always have some many problems with this. Anyways this is the path to the file I want to require

/var/www/vhosts/mysite.com/htdocs/Classes/DBConnection.php

这是包含 require 语句的文件的路径

This is the path to the file that has the require statement

/var/www/vhosts/mysite.com/htdocs/Classes/Forecast/MyFile.php

这是 MyFile.php 中的 require 语句

and this is the require statement in MyFile.php

require_once '../DBConnection.php';

我不断收到无法打开流"错误.如果我放入绝对路径,它工作正常.有没有人看到这里的问题?

I keep getting the "failed to open stream" error. It works fine if I put in an absolute path. Does anyone see the problem here?

推荐答案

如果 /Classes/Forecast/MyFile.php 包含在 /index.php 中的相对路径将来自索引文件.要解决此问题,请使用 __DIR__.然后 require 将与该文件相关.

If /Classes/Forecast/MyFile.php is included from /index.php the relative path will be from the index file. To solve this, use __DIR__. The require will then be relative from that file.

require_once __DIR__.'/../DBConnection.php';

这篇关于包括、要求和相对路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆