include_once, relative path in php

巧了我就是萌 提交于 2019-12-19 16:52:20

问题


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

回答1:


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'



回答2:


Things like realpath() and __DIR__ are your friends when it comes to creating paths in PHP.

Realpath http://php.net/manual/en/function.realpath.php

Magic constants http://php.net/manual/en/language.constants.predefined.php

Since include and require are language constructs (rather than functions) they don't need brackets. Generally speaking you'd use include to include "templates" (output files) and require to include PHP files such as classes.

So you could use something like:

$sPath = realpath( __DIR__ . '/../relative/path/here');
if($sPath) { require_once $sPath; }

(use dirname(__FILE__) instead of __DIR__ on PHP < 5)

It's worth evaluating the path before attempting to require the file as realpath() returns false if the file doesn't exist and attempting to require false; spaffs out a PHP error.

Alternatively you can just use absolute paths like:

require /home/www/mysite.com/htdocs/inc/somefile.php




回答3:


For the beginners this will help to understand. Use simply the following based on your relative path:

//include file directly from parent directory:
include_once(dirname(__FILE__) . '/../connect.php');

OR

//include file from parent's child directory:
include_once(dirname(__FILE__) . '/../StoredProcedure/connect.php');

OR

//include file from own child directory:
include_once('StoredProcedure/connect.php');

OR

//include sibling files from same directory:
include_once('connect.php');



回答4:


Do something like require(dirname(__FILE__) . '[RELATIVE PATH HERE]');




回答5:


The other guys have answered your question correctly, however I thought I'd contribute something else: autoloaders.

Autoloaders allow you to define a function that will automatically include certain files, when you attempt to use a class.

The documentation is here and can explain it better than I can: http://php.net/manual/en/language.oop5.autoload.php

Would really recommend using them, will save you time and is better practice.



来源:https://stackoverflow.com/questions/7835948/include-once-relative-path-in-php

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!