简体   繁体   English

包括不同目录中的多个文件php

[英]including multiple files in different directories php

I am have trouble including files containing functions. 我在包含包含功能的文件时遇到了麻烦。 I have a file called blog.php that runs the following: 我有一个名为blog.php的文件,该文件运行以下命令:

<?php
require 'db.php';
require 'functions.php';

//Connect to the DB
$conn = Blog\DB\connect($config);
if ( !$conn ) die('Problem connecting to the db.');

The blog.php file is in the blog folder. blog.php文件位于blog文件夹中。 Inside the blog folder is an admin folder containing an index.php file with the following code: 在Blog文件夹内是一个admin文件夹,其中包含带有以下代码的index.php文件:

<?php
require '../blog.php';

This should allow me to call the blog.php which then calls the db.php file which contains my database connection functions but I am getting an error that the connect function is undefined . 这应该允许我调用blog.php,然后再调用db.php文件,该文件包含我的数据库连接函数,但是我收到一个错误,指出连接函数未定义。 It's trying to require db.php from the blog>admin folder, not from where the blog.php file is located (which is the same directory as db.php)? 它试图从blog> admin文件夹而不是从blog.php文件所在的位置(与db.php位于同一目录)中请求db.php? How can I fix this? 我怎样才能解决这个问题?

You have a couple of options here - either by changing your relative links to properly reach up to the folder they are supposed to be in, or by changing the current working directory to the appropriate folder using the chdir() method. 您可以在此处选择两个选项-通过更改相对链接以正确到达它们应该位于的文件夹中,或使用chdir()方法将当前工作目录更改为适当的文件夹。 You could also use absolute paths. 您也可以使用绝对路径。

How you should change your relative links is dependent on where in the directory structure you are accessing the file from... 您应该如何更改相对链接取决于您在目录结构中访问文件的位置。

Assuming you're hitting your blog file something like this: 假设您正在访问博客文件,如下所示:

http://www.site.com/blog/blog.php http://www.site.com/blog/blog.php

And that your Admin folder is Inside the blog folder; 并且您的Admin文件夹位于blog文件夹内;

I would include the files like so: 我将包含如下文件:

include "admin/db.php";
include "admin/functions.php";

Again - assuming that both of these files are inside of the admin folder inside of the blog folder; 再次-假设这两个文件都在Blog文件夹内的admin文件夹内;

IE root/blog/admin/db.php IE root /博客/admin/db.php

This is how I am interpreting what you are saying... 我就是这样解释你在说什么...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM