包含的php文件在移动文件子文件夹后不起作用 [英] Included php files wont work after moved files subfolder

查看:107
本文介绍了包含的php文件在移动文件子文件夹后不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对php include部分有疑问.为了充分说明问题,我为您创建了一个测试页.

I have a problem with php include section. In order to fully explain the problem, I created a test page for you.

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

First I want to show the schema of the files to you. Also you can download the test files from this LINK and you can test it online TEST LINK

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

As you can see, there is a subfolder in the htdocs (root) file and all php files in there. Now I'll show you the php code within the file, respectively.

appname/index.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>  

appname/content/test.php

appname/content/test.php

<?php 
include_once 'includes/config.php';
echo $text.'</br>';
echo $worked.'</br>';
?>

appname/includes/config.php

appname/includes/config.php

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

当您打开 测试链接 时,LoadPage(); javascript函数将在内容文件中调用test.php并将其显示在#testBox中.首先,您不会在index.php中的#testBox中看到任何内容.因为config.php不能包含在test.php中.

When you open the TEST LINK, LoadPage(); javascript function will call test.php in the content file and display it in #testBox. First you will not see anything in #testBox from index.php . Because config.php can not be included from test.php .

我知道如果我像这样include_once '/appname/includes/config.php';test.php更改此行include_once 'includes/config.php';,那么问题将得到解决.

I know if I change this line include_once 'includes/config.php'; from test.php like this include_once '/appname/includes/config.php'; then problem will be fix.

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

But if the pages multiply and, I want to use the files in the root (htdocs or www) folder, I need to delete appname (subfolder name) => include_once 'appname/includes/config.php'; from all files. It will be a big problem when these files multiply.

实际上问题是:

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

How can we include php files without specifying the full path to the include, when the application's path relative to the DOCUMENT_ROOT is variable or unknown and include_path cannot be reliably modified by all application users?

推荐答案

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

This is sometimes a problem with includes when you're not using the absolute path on the system.

说明

取决于PHP的运行方式可能会影响include& require的工作方式,如果PHP在appname目录中运行,则可以通过在连接器中得知php在appname目录中运行而正常工作.美好的.但是,例如如果运行PHP www-data@/usr/bin/# php /var/www/somesite.com/htdocs/appname/index.php,则路径可能会中断.

Depending on how PHP is running could affect the way include&require work, if PHP is running from inside the appname directory it will work fine if php is told it's running inside the appname directory via a connector it's fine. however, if PHP is run for example www-data@/usr/bin/# php /var/www/somesite.com/htdocs/appname/index.php the path can be broken.

修复

如果您首先使用define("FS_ROOT", realpath(dirname(__FILE__)));而不是ther是index.php中的命名空间,则可以使用include FS_ROOT."/includes/config.php";,这意味着从系统根目录使用文件路径,因此将其评估为

if you use define("FS_ROOT", realpath(dirname(__FILE__))); as the first thing other than if ther is a namespace inside index.php you can then use include FS_ROOT."/includes/config.php"; this means file paths are used from the root of the system so it gets evaluated to include "/var/www/somesite.com/htdocs/appname/index.php"

为什么会有所不同

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

This differs from ROOT_PATH as ROOT_PATH is sometimes set by PHP configuration by web hosts and this could be the problem. as the PHP execution path could be wrong casing the PHP host application to look in the wrong place for includes and requries.

这也意味着任何includerequire都不应使用../,因为您应该知道代码库中的路径.

This also means no include or require should ever be using ../ as you should know the path from your codebase.

您的应用名称/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>

您的应用名称/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>';
?>

理想情况下,您应该经过引导程序和.htaccess,这样就不必在每次加载的文件中都重新定义FS_ROOT.

Ideally, you should go through a bootstrap and .htaccess so you don't have to change redefine the FS_ROOT in every file loaded.

您可以通过确保在apache中启用mod_rewrite来做到这一点

you can do this by making sure mod_rewrite is enabled in apache

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

create file .htaccess in appname folder

RewriteCond %{REQUEST_URI} \.(php)$
RewriteRule .* bootstap.php [L]

创建bootstrap.php

create 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
}

这意味着您不需要配置的包含,因为它被自动包含在该请求的脚本被希望之前,这只是基本概述,并不是100%安全的,我建议您阅读有关如何使用MVC的建议,它们的工作方式将使您对按需加载文件和需要文件有更好的了解.

this means you don't require the include for the config as it's automaticly included before the script for that request is wanted this is just a base outline and is not 100% secure i would recomended reading up on how to use MVC's and how they work it will give you a better understanding of loading files on demand and requiring files.

这篇关于包含的php文件在移动文件子文件夹后不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆