为什么这些相对路径在 php 中不起作用
我的应用程序结构如下
application -> views -> templates
// some more files
page.php
-> controllers
home.php
-> models
items.php
router.php
index.php
第一种情况:
Index.php
include 'application/routes.php';
Routes.phpcontrollers
require "controllers/home.php";
/home.php
require '/application/models/clusters.php'; //works
require 'application/models/clusters.php'; //works
require '../models/clusters.php'; //doesn't work
为什么第一行有效但最后一行无效?
第二种情况:
Index.php
include 'application/views/page.php';
Page.php
glob("application/views/templates/*.php") // array of files
glob("templates/*.php") // empty array
我认为我对 php 中路径如何工作的理解有问题,但我不知道它是什么。有时路径似乎与当前脚本相关,有时又与index.php相关,但不一定与我使用 /
启动路径相关
My app structure is as follows
application -> views -> templates
// some more files
page.php
-> controllers
home.php
-> models
items.php
router.php
index.php
First case:
Index.php
include 'application/routes.php';
Routes.php
require "controllers/home.php";
controllers/home.php
require '/application/models/clusters.php'; //works
require 'application/models/clusters.php'; //works
require '../models/clusters.php'; //doesn't work
Why do the first lines work but not the last?
Second case:
Index.php
include 'application/views/page.php';
Page.php
glob("application/views/templates/*.php") // array of files
glob("templates/*.php") // empty array
I think there's something wrong with my understanding of how paths work in php, but I can't figure out what it is. Sometimes paths seem to be relative to the current script, adn other times relative to index.php, but not necessarily tied to when I start the path with /
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
请尝试这个:
问题是 PHP 中的路径始终相对于第一个文件路径,在本例中为 index.php。所以你必须包含目录“application”。
另一种方法是使用 set_include_path: http://php.net/manual/ pt_BR/function.set-include-path.php
编辑
要查看您的 include_path:
Please try this:
The problem is that paths in PHP are always relative to the first file path, in this case index.php. So you have to include the directory 'application'.
The alternative is to use set_include_path: http://php.net/manual/pt_BR/function.set-include-path.php
EDIT
To view your include_path:
令我惊讶的是,有多少人不理解 include_path 的重要性。
除非您为 include/require 指定了绝对路径(即以 / 开头的路径),否则 PHP 使用 include_path 依次处理每个包含路径条目来尝试定位文件。通常,包含路径中的第一个条目是 .表示当前目录(如果在脚本中的该点执行,则由 getcwd() 返回)。
It always amazes me how many people don't understand the significance of include_path.
Unless you have specified an absolute path for your include/require, (i.e. a path that begins with a /) then PHP uses include_path to try and locate the file by processing each include path entry in turn. Typically, the first entry in the include path is . meaning the current directory (as returned by getcwd() if it were executed at that point in the script.
试试这个
try this one