我正在构建PHP CMS,但遇到变量问题。我希望能够在外部文件vars.php中包含所有变量,并且只需将该文件包含在需要变量的每个文件中。变量都与URL和文件夹路径有关。全局变量遍历PHP中的文件和文件夹
我正在运行的问题是,如果我将变量设置为完整的URL(例如:$uploadDir = "http://www.example.com/uploads/";
),脚本不会做他们应该做的。如果我使用绝对路径(例如:$uploadDir = "/uploads/";
或$uploadDir = "/full/server/path/uploads/";
),也是如此。
如果我使用完整的网址,它似乎至少尝试工作,但没有。如果我使用完整的服务器路径或绝对路径,那么我得到一些关于该路径的错误不被允许。这些文件都存储在/edit/
或/edit/(sub-folder-name)/
,如果有帮助。
我会寻找一些代码示例,我将使用它,并尽快更新这篇文章。
谢谢。
UPDATE:
一些从这个代码片断的变量不包括在内,只是没想到我应该发表一个巨大的PHP脚本。
的$pageDir
例子:
// The below 2 lines are actually in vars.php, which is included in this file.
$pageDir = "http://www.example.com/edit/pages/";
$url = "http://www.example.com/";
if ($_POST["page"] && $execute == TRUE) {
$live = $_GET["p"] . ".php";
// The below line is how this used to be set up.
// $handle = fopen("pages/$page", "w");
// The below line is how the new version should be set up.
$handle = fopen("$pageDir/$page", "w");
fwrite($handle, $_POST["page"]);
fclose($handle);
// The below line is how this used to be set up.
// echo("<p>Page successfully saved. <a href=\"../$live\" target=\"_blank\">Click here to view this page.</a></p>\n");
// The below line is how the new version should be set up.
echo("<p>Page successfully saved. <a href=\"$url/$live\" target=\"_blank\">Click here to view this page.</a></p>\n");
$execute = FALSE;
}
您使用URL而不是服务器路径对打开和保存文件有点混淆。 (和[这可能会帮助](http://stackoverflow.com/questions/4369/how-to-include-php-files-that-require-an-absolute-path)。) – 2011-12-17 16:56:12