PHP Path to Include Files

巧了我就是萌 提交于 2020-01-06 08:33:16

问题


I have a site with an index.php. The index.php has a number of include files like

 <?php include_once('scripts/php/index_mainImageJoin.php');?>

I've created a new folder off the root called 'extrapages' and I'm going to have pages in there that have information relating to the site. I've added the include_once files like above and change the path and these hook up fine.

The problem I'm find is paths within the include files fail. eg: if an image or another include file is within the include it fails when run from the 'extrapages' folder. Pathing issue.

Is there a good way to deal with this? Can I change/set the path to the site root (www) for pages under 'extrapages' to one folder down by chance?

I could move these pages onto the root and they would run fine but I really don't want all the clutter on the root of the site.

Any ideas & thx


回答1:


The key to any path problem is called absolute path

  • while creating hyperlinks for your site (including image sources), always start it from / followed by full correct path. And it never fail you.

  • same for the filesystem calls: always use absolute path. Your server usually provides you with very handy variable called $_SERVER['DOCUMENT_ROOT'] contains the point where filesystem meet web-server, pointing to your web root directory.

So, when called from anywhere in your site,

include $_SERVER['DOCUMENT_ROOT'].'/scripts/php/index_mainImageJoin.php';

will point always to the same location




回答2:


Just add your include path once (somewhere at the beginning or in a config file) using set_include_path(), see the manual. Use an absolute path (not relative; can utilize dirname(__FILE__)) and it should work all the time.




回答3:


you should use dirname and the __FILE__ by using this both constant you should be able to include file relative to the current file instead of the php script called by the web server.

for example

include_once dirname(__FILE__) . '/../include.php';

dirname: would return the directory part of a path

__FILE__: is a magic constant, it's replaced by the path of the current file.

The only problem with doing such thing you lock the structure of your project but most of the times it's acceptable.




回答4:


If you're on PHP 5.3.0 or newer, you can (instead of what RageZ) suggested, use just __DIR__ (a newly defined magic constant).

example:

include __DIR__ . '/../include.php';

Now, this doesn't help when you want to avoid ../ and mapping out your includes. There's a better way to do it, though - in all front-end files (which should be the ONLY user-accessible PHP files) you define a constant which provides the root path of your script (and not of the current file).

For example:

index.php

<?php
define('MYSCRIPT_ROOT', dirname(__FILE__));
// or in php 5.3+ ...
define('MYSCRIPT_ROOT', __DIR__);

// ... do some stuff here

include MYSCRIPT_ROOT . '/includes/myinclude.php';

Now let's say we want to include a file in our includes directory.

Let's do it in includes/myinclude.php, and include the file includes/myotherinclude.php

includes/myinclude.php

<?php
if(!defined('MYSCRIPT_ROOT')) exit; // prevent direct access - ALWAYS a good idea.

// ... do stuff here or something

include MYSCRIPT_ROOT . '/includes/myotherinclude.php';

Keep in mind that the include paths should be directly relative to the root directory of the project itself, not to just one of the front-end files. If you have a front-end file in a subdirectory, you need to back out to the project root itself when defining the constant.

example:

subdirectory/index.php

index.php

<?php
define('MYSCRIPT_ROOT', dirname(dirname(__FILE__)));
// or in php 5.3+ ...
define('MYSCRIPT_ROOT', dirname(__DIR__));

// ... do some stuff here

include MYSCRIPT_ROOT . '/includes/myinclude.php';

All we do here is add a dirname() call, which takes off a directory in the path. See: http://us.php.net/manual/en/function.dirname.php



来源:https://stackoverflow.com/questions/7544784/php-path-to-include-files

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