0

階層のさらに下のフォルダーにあるファイルを含める方法は知っていますが、元に戻す方法を見つけるのに苦労しています。set_include_path を使用して、2 レベル上のパスに関連するすべての追加のインクルードをデフォルトにすることにしましたが、それを書き出す方法が少しもわかりません。

PHP のパス参照について詳しく説明しているガイドはどこかにありますか?

4

2 に答える 2

2

私はdirnameを使用して現在のパスを取得し、これをベースとして使用して将来のすべてのパス名を計算する傾向があります。

例えば、

$base = dirname( __FILE__ ); # Path to directory containing this file
include( "{$base}/includes/Common.php" ); # Kick off some magic
于 2008-11-09T05:26:45.693 に答える
1

絶対パスを使用して参照する方がおそらく簡単です。

set_include_path('/path/to/files');

このようにして、将来のすべてのインクルードの基準点が得られます。インクルードは、それらが呼び出された時点に関連して処理されるため、特定のシナリオでは少し混乱する可能性があります。

例として、サンプル フォルダ構造 ( /home/files)を指定します。

index.php
test/
  test.php
test2/
  test2.php

// /home/files/index.php
include('test/test.php');

// /home/files/test/test.php
include('../test2/test2.php');

index.php を呼び出すと、次のファイルを含めようとします。

/home/files/test/test.php // expected
/home/test2/test2.php // maybe not expected

これはあなたが期待するものではないかもしれません。test.php を呼び出すと/home/files/test2/test.php、期待どおりに呼び出されます。

結論として、インクルードは元の呼び出しポイントに対して相対的になります。明確にするために、これset_include_path()は相対的な場合にも影響します。次の点を考慮してください (同じディレクトリ構造を使用)。

<?php
// location: /home/files/index.php
   set_include_path('../'); // our include path is now /home/

   include('files/test/test.php'); // try to include /home/files/test/test.php
   include('test2/test2.php'); // try to include /home/test2/test2.php
   include('../test3.php'); // try to include /test3.php
?>
于 2008-11-09T03:19:13.003 に答える