开发者

Problem getting PHP include working with files in different folders

开发者 https://www.devze.com 2023-03-14 09:35 出处:网络
I\'m a total PHP noob and am using a pretty simple PHP include: <?php include(\"~head.php\"); ?>

I'm a total PHP noob and am using a pretty simple PHP include:

<?php include("~head.php"); ?>

to do a bit of templating for a website (to achieve common headers, footers, menus for all my pages).

  • NOTE: The tilde (~) is simply there to make the directories easier to look at (pushes main files to the top of the list when sorted alphabetically)

It's working great for files that are in the same directory but when I reference a file outside of a directory, like so:

<?php include("../~head.php"); ?>

However, it simply doesn't seem to be finding the file as the header is clearly not being pulled into the markup.

Conversely, if I reference the file with a full url, e.g.

<?php include("http://example.com/~head.php"); ?>

I get the following error code on my page.

Warning: include() [function.include]: URL file-access is disabled in the server configuration in /home/content/65/7392565/html/bikini/angela_bikini.php on line 1

Warning: include(http://example.com/~head.php) [function.include]: failed to open stream: no suitable wrapper could be found in /home/content/65/7392565/html/products/product_a.php on line 1

Warning: include() [function.include]: Failed opening 'http://example.com/~head.php' for inclusion (include_path='.:/usr/local/php5/lib/php') in /home/content/65/7392565/html/products/product_a.php on line 1

Strangely, the "../file.php" syntax works for non-header files (e.g. the include I'm using for the menu).

As such code's gotten to be a bit of a fragmented mess and is difficult to maintain changes across all the different pages. Any thoughts or solutions would be very much appreciated. I really am a noob tho so I probably won't be able to wrap my head around anything too fa开发者_开发百科ncy. : )

Thanks for your time.

Jon


Rather than using only the ../ to get the directory above, a construct like this will create the full filepath:

// Assuming you are including from the root
$application_path = dirname(__FILE__);
include("$application_path/../header.php);

Typically I'll do this by defining a constant, rather than using a variable.

define('APP_PATH', dirname(__FILE__));

Use this as:

// Assuming you are including at the file root:
define('APP_PATH', dirname(__FILE__));
include(APP_PATH . "/include/head.php");

// Assuming you are including from /include (one directory in)
// append a "/../" onto the end to indicate that the application
// root is one directory up from the currently executing file.
define('APP_PATH', dirname(__FILE__) . "/../");
include(APP_PATH . "somefile_at_the_root.php");


You have to be careful with the tilde! Under UNIX-like operating systems, the tilde is a shortcut to your home directory. If maybe the Apache server runs under the account www, your file-reference could be interpreted like this:

/home/www/head.php

And for the approach of using the full URL, the error says all:

URL file-access is disabled in the server configuration

Ignoring that it isn't best practice to use full URLs (because your folder structure could change etc.), you have to enable allow_url_include in your php.ini (see PHP.net).

If you really want to have your important files on top, you could use the underscore _.

0

精彩评论

暂无评论...
验证码 换一张
取 消