duan36000 2011-12-17 16:41
浏览 43
已采纳

PHP中文件和文件夹的全局变量

I'm building a PHP CMS, but having trouble with variables. I want to be able to have all the variables in an external file, vars.php, and just include that file in every file that requires the variables. The variables all have to do with URLs and folder paths.

The problem I'm running in to is that if I set the variables to full URLs (ex: $uploadDir = "http://www.example.com/uploads/";), the scripts don't do what they should. Same thing if I use absolute paths (ex: $uploadDir = "/uploads/"; or $uploadDir = "/full/server/path/uploads/";).

If I use full URLs, it seems like it at least tries to work, but doesn't. If I use the full server path or absolute path, I get some error about that path not being allowed. The files are all stored in /edit/ or /edit/(sub-folder-name)/, if that helps.

I'll look around for some code examples where I would be using this, and update this post ASAP.

Thanks.

UPDATE:

Some of the variables from this code snippet aren't included, just didn't think I should post a huge PHP script.

Example of $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>
");    
    // 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>
");    
    $execute = FALSE;
}
  • 写回答

2条回答 默认 最新

  • douyan2970 2011-12-17 16:55
    关注

    Don't use URLs for file-system functions. That will make them do a HTTP request, not load the files from the filesystem.

    Also if you use /absolute paths, then that will refer to the filesystem root, not to your webservers document root. PHP filesystem functions do not operate on virtual path names, but on the real system file structure.

    You can however use:

     $pageDir = "/edit/pages/";
     readfile("$_SERVER[DOCUMENT_ROOT]/$pageDir/$page");
    

    That's probably what you want.

    Other caveats: despite the meme, there is nothing wrong with global vars. You should however not swamp the shared scope with dozens of them. Instead define an array like $dir["page"] for folders. Or define() constants.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 高价求中通快递查询接口
  • ¥15 解决一个加好友限制问题 或者有好的方案
  • ¥15 关于#java#的问题,请各位专家解答!
  • ¥15 急matlab编程仿真二阶震荡系统
  • ¥20 TEC-9的数据通路实验
  • ¥15 ue5 .3之前好好的现在只要是激活关卡就会崩溃
  • ¥50 MATLAB实现圆柱体容器内球形颗粒堆积
  • ¥15 python如何将动态的多个子列表,拼接后进行集合的交集
  • ¥20 vitis-ai量化基于pytorch框架下的yolov5模型
  • ¥15 如何实现H5在QQ平台上的二次分享卡片效果?