1

私はこのようなフォルダ構造を持っています:

  public_html
     /index.php   
  outside
     /other.php

public_html/index.phpフォルダにあるインクルードファイルが欲しいですoutside。だから、私はファイル内の外部フォルダパスを定義したいと思いpublic_html/index.phpます。私はこれをそうします:

index.php

 $doc_root_upper = preg_replace("#/[^/]+$#", "", $_SERVER["DOCUMENT_ROOT"]);
 $outside_folder = $doc_root_upper."/outside";
 include outside_folder.'/other.php';

これは機能しますが、フォルダーパスの外側にwebrootを定義するためのより良い方法がありますか?

4

3 に答える 3

2

My personal preference is to start my init.php file (included by php.ini's auto_prepend_file directive) with chdir($_SERVER['DOCUMENT_ROOT']).

This means that all of my includes and other file functions are done from the document root.

In your case, this means you can then do include ../outside/other.php reliably.

于 2013-01-29T19:06:12.967 に答える
2

Use a relative path like this:

Include("../outside/other.php");

../ means up one directory.

于 2013-01-29T19:07:04.903 に答える
1

I like to use a chdir() to get the explicit paths and then add them to the include path. I tend to have a file called init.php that contains the following:

$path = get_include_path(); // get the current path
$oldcwd = getcwd(); // get the current path

// change to outside of the document root
chdir($_SERVER['DOCUMENT_ROOT'] . "/.."); 
$newcwd = getcwd(); // get the new working directory

// build the new path. Using hte _SEPARATOR constants ensures 
// that it works on all platforms.
$newpath = $path . PATH_SEPARATOR . $newcwd . DIRECTORY_SEPARATOR . "outside"; 

// Set the new path
set_include_path($newpath);

chdir($oldcwd); // change back to the original working directory
于 2013-01-29T19:11:47.503 に答える