简体   繁体   中英

undefined function error when calling it in another directory

I have another directory(admin) there i have an index file which require blog file and i got an error its say's Call to undefined function Blog\\DB\\connect() but what really confuse me is that it never gives me an error when i require it in the root directory files( index or single ) it runs very fine.

admin/index.php code:

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

view('admin/create');
?> 

blog.php code:

    require 'functions.php';
    require 'config/config.php';
    require 'db.php';

     use Blog\DB;
 $conn = DB\connect($config['DB_USERNAME'],$config['DB_PASSWORD'],$config['DATABASE']);
 if (!$conn) die('Could not Connect');

I have spent 2 days finding the reason for this error if anyone can enlighten that would be great.

在此处输入图片说明

You need to select the correct directory. If you're not in the directory right below where the file is, you need to go up another level

require '../../blog.php';

Or, you can specify the path starting from DOCUMENT_ROOT, so that you never have to calculate how many levels to go up:

require $_SERVER['DOCUMENT_ROOT'].'/path/to/blog.php'

That's because require uses the calling scripts folder (which is admin/ ). Change your require in blog.php to:

require dirname(__FILE__) . '/functions.php';
require dirname(__FILE__) . '/config/config.php';
require dirname(__FILE__) . '/db.php';

Using $_SERVER['DOCUMENT_ROOT'] only works it the script is called through a web server and not from CLI.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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