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 docker环境配置
  • ¥20 绿盟安全扫描--检测到目标站点存在javascript框架库漏洞
  • ¥30 Android STD快速启动
  • ¥15 如何使用simulink建立一个永磁同步直线电机模型?
  • ¥30 天体光谱图的的绘制并得到星表
  • ¥15 PointNet++的onnx模型只能使用一次
  • ¥20 西南科技大学数字信号处理
  • ¥15 有两个非常“自以为是”烦人的问题急期待大家解决!
  • ¥30 STM32 INMP441无法读取数据
  • ¥15 R语言绘制密度图,一个密度曲线内fill不同颜色如何实现