【问题标题】:Confused in writing paths in php在php中编写路径时感到困惑
【发布时间】:2012-05-03 23:40:25
【问题描述】:

我很困惑在 include 中编写路径。

这里有一些细节

我有一个文件 index.php 在这里:

localhost/widget/explore/staff/index.php

并希望在此处包含一个名为 constants.php 的文件:

localhost/widget/includes/constants.php

告诉我需要的路径怎么写

include '?';

【问题讨论】:

  • 您是在尝试使用include() PHP 的核心功能还是在谈论include_path PHP 配置选项?
  • @webbandit 从技术上讲,include 是一个预处理器指令,而不是一个函数,这就是为什么我更喜欢它不带括号,OP 的编写方式:-]
  • 警告:require_once(connection.php) [function.require-once]:无法打开流:C:\Users\Yousuf\Desktop\Web\localhost\widget_corp\ 中没有这样的文件或目录explore\staff\index.php 第 6 行
  • 仍然报错 grrr
  • 现在它的工作认为每个人都特别感谢您的帮助

标签: php path include


【解决方案1】:
include("..\\..\\includes\\constants.php");

或者试试这个;

include("C:\\Users\\Yousuf\\Desktop\\Web\\localhost\\widget_corp\\includes\\constants.php");

【讨论】:

  • include('/widget/includes/constants.php'); - 我认为/widget/ 文件夹不在根目录中))
  • 不工作仍然收到此错误:警告:require_once(connection.php) [function.require-once]:无法打开流:C:\Users\Yousuf\Desktop 中没有这样的文件或目录\Web\localhost\widget_corp\explore\staff\index.php 在第 6 行
  • 这样写,php使用apache文档根目录,而不是hdd根目录。 (你不能做一个 include('/etc/passwd'); 你必须做一个 include('../../../../../ etc/passwd'); 例如。)编辑:这是假设 apache 和 php 具有典型的安全配置。
  • 来自 PHP dosc 站点:If a path is defined — whether absolute (starting with a drive letter or \ on Windows, or / on Unix/Linux systems) or relative to the current directory (starting with . or ..) — the include_path will be ignored altogether. For example, if a filename begins with ../, the parser will look in the parent directory to find the requested file..
  • @yusufiqbalpk 我只是不希望你为我写一条路在向别人寻求帮助时,有一点礼貌会大有帮助。
【解决方案2】:

您也可以使用chdir() 来更改您的工作目录,尽管它并不美观并且可能会造成混淆。

【讨论】:

  • 请具体回答
【解决方案3】:

首先要记住的是,确保所有包含敏感信息(数据库登录名/密码等)的文件都存储在应用的文档路径之外。你不想让别人很容易地从网络浏览器下载这种类型的文件。

考虑到上述情况,看看你的 php.ini 文件。具体来说,搜索“include_path”值。这列出了使用includerequire 时搜索文件的每个文件夹。

例如,如果您的搜索路径类似于:include_path=".:/usr/local/lib/php",那么您可以将常量文件放入/usr/local/lib/php,然后使用include 'constants.php';

更好的办法是创建一个单独的文件夹(例如 /usr/local/lib/myConstants)并将其添加到 include_path 值的末尾 (include_path=".:/usr/local/lib/php:/usr/local/lib/myConstants")

【讨论】:

  • 先生,我说的是 include() 核心函数,比如 require()
  • @yusufiqbalpk DaveyBox 正在讨论您可以使用该功能的方式。
  • 如何包含或要求文件取决于您的 include_path 值。如果找不到文件,include 将继续,require 将停止执行。为了让你工作,include ('../../includes/constants.php'); 应该检索文件,但我真的不建议让你的常量文件像这样访问 - 处理 PHP 的一个快速失败(有人在删除 PHP 处理后重新启动服务器)并且有人会看到页面的源代码,其中包含有关如何获取常量文件的说明。
  • PHP 可以访问整个文件系统,因此将文件存储在其他地方只是默默无闻的安全性。最好确保文件不能被 Web 服务器读取,并将其存储在
  • 我认为 DaveyBoy 在这里是正确的,尽管可能有点过度保护。但是 nyson 也有一半是正确的(php 可以配置为只访问文件系统的一部分),如果常量文件是可读的,它总是可读的,不管你把它放在哪里。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-08-01
  • 1970-01-01
  • 2016-02-10
相关资源
最近更新 更多