Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ajax/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在php的子文件夹中设置处理脚本_Php_Ajax_Include_Directory - Fatal编程技术网

在php的子文件夹中设置处理脚本

在php的子文件夹中设置处理脚本,php,ajax,include,directory,Php,Ajax,Include,Directory,我正在尝试清理我的网站文件夹结构。我在根目录中有太多的处理脚本。原因是如果我将它们移动到子文件夹并从那里调用它们,它们将无法工作 例如 root directory - images folder - css folder - js folder - includes folder - admin folder index.php ajax.php 假设我将ajax.php移动到includes文件夹,它将如下所示 root directory - images fo

我正在尝试清理我的网站文件夹结构。我在根目录中有太多的处理脚本。原因是如果我将它们移动到子文件夹并从那里调用它们,它们将无法工作

例如

root directory
  - images folder
  - css folder
  - js folder
  - includes folder
  - admin folder
index.php
ajax.php 
假设我将ajax.php移动到includes文件夹,它将如下所示

root directory
  - images folder
  - css folder
  - js folder
  - includes folder
    - ajax.php
  - core folder
  - template folder
index.php
现在,在每个页面上,我都需要一个init.php。例如,在ajax.php中

<?php require_once 'core/init.php'; ?>
// rest of the code
如果我将ajax.php移回根目录,它将正常工作

在index.php上,我就是这样调用ajax.php的

<a href="includes/ajax.php?id="324"></a>

core/init.php在“templates”文件夹中需要时可以正常工作


也许有人能告诉我这个问题的解决办法

尝试使用PHP的
chdir
函数。这将更改PHP环境所处的工作目录,从而更改include函数所查看的相对路径。理想情况下,您希望使用项目的根调用
chdir
,以便在执行
require\u一次('core/init.php')
您最终不是从
core
目录调用,而是从
目录调用。

您可能会得到一些关于如何最好地构造文件的意见。但是你需要决定你的终点哲学。例如,为什么会有任何名为
includes/*
的web访问端点?在这一点上,该文件真的是一个包含文件或库文件,还是仅仅是一个端点?我的一般理念是,在web accessilbe目录中没有任何不打算用作端点的内容,但再次强调,您将在这里获得许多意见,因此这可能不是一个好问题。我会记住的,太好了。我将检查该函数。谢谢
<a href="includes/ajax.php?id="324"></a>