include_once,在php中的相对路径 [英] include_once, relative path in php

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

问题描述

我有3个文件:home,failed_attempt,login。



文件home和failed_attempt都引用登录文件。



令人讨厌的是他们抛出一个错误,说登录文件不存在。 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,wont。我如何解决这个问题。



我是否通过放置这个../来告诉include上一个页面,因此home.php不需要去一页因此它会抛出异常。



我怎么能这样做,所以这两个文件接受这些包括作为有效..也许不是相对于他们放置..即没有那../



目录结构:

  PoliticalForum 
- > home.php
- > StoredProcedure / connect.php
- > untitled / sanitize_string.php
- >和其他不相关的文件


解决方案

这里有三种可能的解决方案。第二个实际上只是巧妙地使用绝对路径的解决方法。



1:chdir到正确的目录

 <?php 

//检查当前目录中是否存在StoredProcedure文件夹
//当前目录中不存在,将当前
//目录上移一级。
//
//这个while循环将继续向上移动目录树,直到
//当前目录包含StoredProcedure文件夹。
//
while(!file_exists('StoredProcedure'))
chdir('..');

include_onceStoredProcedure / connect.php;
// ...
?>

请注意,只有当您的StoredProcedure文件夹位于任何可能需要的文件的最顶层目录包括它包含的文件。

2:使用绝对路径



现在在你说这不是可移植的之前,它实际上取决于你如何实现它。这里有一个适用于Apache的例子:

 <?php 
include_once $ _SERVER ['DOCUMENT_ROOT']。 /StoredProcedure/connect.php;
// ...
?>

另外,再次使用apache,将以下内容放在根目录的.htaccess中:

  php_value auto_prepend_file /path/to/example.php 

然后在example.php中:

 <?php 

define('MY_DOC_ROOT','/ path / to / docroot');

?>

最后在您的档案中:

 <?php 
include_once MY_DOC_ROOT。 /StoredProcedure/connect.php;
// ...
?>

<3>设置PHP的include_path

请参阅 include_path指令的手动输入项 。如果您无权访问php.ini,则可以在.htaccess中进行设置,假设您使用的是Apache,并且将PHP安装为CGI,如下所示:


$ b $

  php_value include_path'/ path / to / my / includes / folder:/ path / to / another / includes / folder'


I have 3 files: home, failed_attempt, login.

The file home and failed_attempt all refer to login file.

The annoying thing is that they throw a mistake saying that the login file doesnt exist. home will throw an exception if i do this, but failed_attempt wont.

  include_once("../StoredProcedure/connect.php");

include_once("../untitled/sanitize_string.php");

and if I do this:

   include_once("StoredProcedure/connect.php");
   include_once("untitled/sanitize_string.php");

the opposite happens, failed_attempt throws an exception , but home, wont. How do I fix this..

Do I tell the include to go up a page by putting this ../ , and therefore home.php doesnt need to go one page up therefore it throws that exception..

How can I make it so both files accept those inclueds as valid.. perhaps not relative to where they are placed.. i.e. without that ../

Directory structure:

PoliticalForum
->home.php
->StoredProcedure/connect.php
->untitled/sanitize_string.php
->And other irrelevant files

解决方案

Here are three possible solutions. The second are really just work-arounds that use absolute paths in a clever way.

1: chdir into the correct directory

<?php

// check if the 'StoredProcedure' folder exists in the current directory
// while it doesn't exist in the current directory, move current 
// directory up one level.
//
// This while loop will keep moving up the directory tree until the
// current directory contains the 'StoredProcedure' folder.
//
while (! file_exists('StoredProcedure') )
    chdir('..');

include_once "StoredProcedure/connect.php";
// ...
?>

Note that this will only work if your StoredProcedure folder is in the topmost directory of any files that might need to include the files it contains.

2: Use absolute paths

Now before you say this is not portable, it actually depends on how you implement it. Here's an example that works with Apache:

<?php
include_once $_SERVER['DOCUMENT_ROOT'] . "/StoredProcedure/connect.php";
// ...
?>

Alternatively, again with apache, put the following in your .htaccess in the root directory:

php_value auto_prepend_file /path/to/example.php

Then in example.php:

<?php

define('MY_DOC_ROOT', '/path/to/docroot');

?>

And finally in your files:

<?php
include_once MY_DOC_ROOT . "/StoredProcedure/connect.php";
// ...
?>

3: Set PHP's include_path

See the manual entry for the include_path directive. If you don't have access to php.ini, then this can be set in .htaccess, providing you are using Apache and PHP is not installed as CGI, like so:

php_value include_path '/path/to/my/includes/folder:/path/to/another/includes/folder'

这篇关于include_once,在php中的相对路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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