我有 3 个文件:home、failed_attempt、login。
文件 home 和 failed_attempt 均指登录文件。
烦人的是他们抛出一个错误,说登录文件不存在。如果我这样做,家会抛出异常,但尝试失败。
include_once("../StoredProcedure/connect.php");
include_once(“../untitled/sanitize_string.php”);
如果我这样做:
include_once("StoredProcedure/connect.php");
include_once("untitled/sanitize_string.php");
相反的情况发生,failed_attempt 抛出异常,但 home 不会。我该如何解决..
我是否通过放置此 ../ 来告诉 include 向上一页,因此 home.php 不需要向上一页,因此它会抛出该异常。
我怎样才能做到这一点,以便两个文件都接受这些包含的内容为有效..也许与它们放置的位置无关..即没有那个../
目录结构:
PoliticalForum
->home.php
->StoredProcedure/connect.php
->untitled/sanitize_string.php
->And other irrelevant files
像realpath()
and __DIR__
在 PHP 中创建路径时是您的朋友。
Realpath http://php.net/manual/en/function.realpath.php
魔法常数 http://php.net/manual/en/language.constants.predefined.php
由于 include 和 require 是语言构造(而不是函数),因此它们不需要括号。一般来说你会使用include
包括“模板”(输出文件)和require
包含 PHP 文件,例如类。
所以你可以使用类似的东西:
$sPath = realpath( __DIR__ . '/../relative/path/here');
if($sPath) { require_once $sPath; }
(use dirname(__FILE__)
代替__DIR__
在 PHP
在尝试将文件作为要求之前,值得评估路径realpath()
如果文件不存在并尝试返回 falserequire false;
抛出一个 PHP 错误。
或者,您可以只使用绝对路径,例如:
require /home/www/mysite.com/htdocs/inc/somefile.php
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)