包含的php文件在移动文件子文件夹后无法正常工作

Azz*_*zzo -2 javascript php ajax .htaccess jquery

我有一个PHP包含部分的问题.为了充分解释这个问题,我为您创建了一个测试页面.

首先,我想向您展示文件的架构.您也可以从此LINK下载测试文件,您可以在线测试TEST LINK

在此输入图像描述

如您所见,htdocs(根)文件中有一个子文件夹,其中包含所有php文件.现在我将分别向您展示文件中的PHP代码.

APPNAME/index.php文件

<?php include_once 'includes/config.php';?>
<div class="callPage">Click Here</div>
<div id="testBox"></div>

<script type="text/javascript">
  $(document).ready(function(){
      var siteurl = '<?php echo $url;?>';
      $("body").on("click",".callPage", function(){
       $.ajax({
            url: siteurl+'content/test',
            beforeSend: function() {
                //Do something
            },
            complete:function(){
              //Do something 
            },
            success:function(response){
                $("#testBox").html(response);
            }
            });
    });
     function LoadPage(){
       $.get(siteurl+'content/test', function(data) {
          $('#testBox').html(data);
       });
    }
    LoadPage(); 
  });
</script>  
Run Code Online (Sandbox Code Playgroud)

应用程序名称/内容/ test.php的

<?php 
include_once 'includes/config.php';
echo $text.'</br>';
echo $worked.'</br>';
?>
Run Code Online (Sandbox Code Playgroud)

应用程序名称/包括/ config.php中

<?php 
$url = 'http://localhost:8888/';
$text = 'Well Come! How are you today ?';
$worked = 'It is working :)';
?>
Run Code Online (Sandbox Code Playgroud)

当您打开TEST LINK时,LoadPage();javascript函数将调用test.php内容文件并显示在其中#testBox.首先,你将看不到任何东西#testBoxindex.php.因为config.php不能包括在内test.php.

我知道如果我改变这一行include_once 'includes/config.php';test.php这样的include_once '/appname/includes/config.php';那么问题将修复.

但是如果页面成倍增加,我想使用root(htdocs或www)文件夹中的文件,我需要include_once 'appname/includes/config.php';从所有文件中删除appname(子文件夹名称)=> .当这些文件成倍增加时,这将是一个大问题.

实际上问题恰恰是:

当应用程序的路径相对于DOCUMENT_ROOT变量或未知并且include_path无法被所有应用程序用户可靠地修改时,如何在不指定包含的完整路径的情况下包含php文件?

Mar*_*ker 7

当您不在系统上使用绝对路径时,这有时会出现问题.

说明

根据PHP运行的方式可能会影响方式includerequire工作,如果PHP从appname目录中运行,如果通过连接器告诉php在appname目录中运行,它将正常工作.但是,如果运行PHP,例如www-data@/usr/bin/# php /var/www/somesite.com/htdocs/appname/index.php路径可能会被破坏.

固定

如果你使用的define("FS_ROOT", realpath(dirname(__FILE__)));是第一件事,而不是如果是index.php中的命名空间你可以使用include FS_ROOT."/includes/config.php";这意味着从系统的根目录使用文件路径,所以它被评估为 include "/var/www/somesite.com/htdocs/appname/index.php"

为何这有所不同

这与ROOT_PATH不同,因为ROOT_PATH有时由Web主机通过PHP配置设置,这可能是问题所在.因为PHP执行路径可能是错误的,所以要求PHP主机应用程序在错误的位置查找包含和请求.

这也意味着没有include或者require应该使用,../因为您应该知道代码库中的路径.

你的appname/index.php

<?php define("FS_ROOT", realpath(dirname(__FILE__)));
include_once FS_ROOT.'includes/config.php';?>
<div class="callPage">Click Here</div>
<div id="testBox"></div>

<script type="text/javascript">
  $(document).ready(function(){
      var siteurl = '<?php echo $url;?>';
      $("body").on("click",".callPage", function(){
       $.ajax({
            url: siteurl+'content/test',
            beforeSend: function() {
                //Do something
            },
            complete:function(){
              //Do something 
            },
            success:function(response){
                $("#testBox").html(response);
            }
            });
    });
     function LoadPage(){
       $.get(siteurl+'content/test', function(data) {
          $('#testBox').html(data);
       });
    }
    LoadPage(); 
  });
</script>
Run Code Online (Sandbox Code Playgroud)

你的appname/content/test.php

<?php 
# as this file is loaded directly and is not in the root directory
# we apend the dirname result with ../ so realpath can resolve the root directory for this site
define("FS_ROOT", realpath(dirname(__FILE__)."../"));
include_once FS_ROOT.'includes/config.php';
echo $text.'</br>';
echo $worked.'</br>';
?>
Run Code Online (Sandbox Code Playgroud)

理想情况下,您应该通过引导程序和.htaccess,这样您就不必更改重新定义FS_ROOT加载的每个文件.

你可以通过确保mod_rewrite在apache中启用来完成此操作

.htaccess在appname文件夹中创建文件

RewriteCond %{REQUEST_URI} \.(php)$
RewriteRule .* bootstap.php [L]
Run Code Online (Sandbox Code Playgroud)

创建 bootstrap.php

define("FS_ROOT", realpath(dirname(__FILE__)));
include_once FS_ROOT.'includes/config.php';
if(file_exists(FS_ROOT.$_SERVER['REQUEST_URI']){
    include(FS_ROOT.$_SERVER['REQUEST_URI']);
}else{
    // 404
}
Run Code Online (Sandbox Code Playgroud)

这意味着您不需要配置的包含,因为它是在需要该请求的脚本之前自动包含的这只是一个基本大纲并且不是100%安全我会建议阅读如何使用MVC以及它们如何工作它将使您更好地了解按需加载文件和需要文件.