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条)

报告相同问题?

悬赏问题

  • ¥20 iqoo11 如何下载安装工程模式
  • ¥15 本题的答案是不是有问题
  • ¥15 关于#r语言#的问题:(svydesign)为什么在一个大的数据集中抽取了一个小数据集
  • ¥15 C++使用Gunplot
  • ¥15 这个电路是如何实现路灯控制器的,原理是什么,怎么求解灯亮起后熄灭的时间如图?
  • ¥15 matlab数字图像处理频率域滤波
  • ¥15 在abaqus做了二维正交切削模型,给刀具添加了超声振动条件后输出切削力为什么比普通切削增大这么多
  • ¥15 ELGamal和paillier计算效率谁快?
  • ¥15 蓝桥杯单片机第十三届第一场,整点继电器吸合,5s后断开出现了问题
  • ¥15 file converter 转换格式失败 报错 Error marking filters as finished,如何解决?