0

次の場所にあるファイルがあります。

/Library/WebServer/Documents/wordpress/wp-content/themes/directorypress/sidebar-left-big.php

サブディレクトリに別のファイルがあります:

/Library/WebServer/Documents/wordpress/wp-content/themes/directorypress/template_directorypress/_gallerypage.php

そして _gallerypage.php には php が含まれています:

<?php include('../sidebar-left-big.php'); //left sidebar, category navigation and ads ?>

私が得るエラー:

Warning: include(../sidebar-left-big.php) [function.include]: failed to open stream: No such file or directory in /Library/WebServer/Documents/wordpress/wp-content/themes/directorypress/template_directorypress/_gallerypage.php on line 9

Warning: include() [function.include]: Failed opening '../sidebar-left-big.php' for inclusion (include_path='.:') in /Library/WebServer/Documents/wordpress/wp-content/themes/directorypress/template_directorypress/_gallerypage.php on line 9

私にはすべてを正しく行っているように思えます。

おそらく問題は、 _gallerypage.php が別のファイルにインクルードを介して読み込まれているため、../ に対して相対的にエラーが発生することだと思いました。しかし、エラーは sidebar-left-big.php へのパスがどこにあると考えているかについては何も言いません。

4

3 に答える 3

1

使用するinclude dirname(__FILE__).'/../sidebar-left-big.php';

于 2012-07-22T10:06:36.540 に答える
0

はい、あなたは正しいです。

_gallerypage.php別のファイルからを含めると、それ自体に対する相対パスが使用されます。したがって、これを修正する必要があります。

最善の方法は、そのような困難を避けることかもしれません。これを行うにはいくつかの方法があります。同様に、定数でグローバルルートを定義し、それに従ってすべてのものをどこにでも含めます。

例えば:

define("BASE" , "/wordpress"); //Define a base directory

//now whenever you are including and requirng files, include them on the basis of BASE

require(BASE."/admin/.../some.php");
于 2012-07-22T10:09:22.623 に答える
0

次のスニペットを使用して、PHP アプリケーションのルートにファイル (config.php など) として保存します。次に、他のすべての PHP ファイルでこのファイルを参照して、変数 BASE_PATH および TOP_LEVEL_DIR を使用できます。

<?php

/**
 * @def (string) DS - Directory separator.
 */
define("DS","/",true);

/**
 * @def (resource) BASE_PATH - get a base path.
 */
define('BASE_PATH',realpath(dirname(__FILE__)).DS,true);
define('TOP_LEVEL_DIR', public_base_directory());

?>

<?php
function public_base_directory()
{
    //get public directory structure eg "/top/second/third"
    $public_directory = dirname($_SERVER['PHP_SELF']);
    //place each directory into array
    $directory_array = explode('/', $public_directory);
    //get highest or top level in array of directory strings
    $public_base = max($directory_array);

    return $public_base;
}
?>
于 2013-04-30T00:24:06.273 に答える