0

I have a script which has located in /fileupload/index.php. Let's name A. And i want to include a script inside A. Lets name it B. B is located /includes/class/class.product.php

How can i include B inside A using realpath(dirname(FILE)) ?

if A located /index.php I'm including like B like...

require_once realpath(dirname(__FILE__)).'/includes/class/class.product.php';

But main problem is A is located /fileupload/index.php. What should i do ? I don't want to hardcode it. That is why i'm asking.

cihanblog
  • 58
  • 1
  • 9

1 Answers1

0

First, you have to define the SITE_ROOT, like this

// PAGE_FILE = this is the path of the current file
// SITE_ROOT = this is the root of your site
if (DIRECTORY_SEPARATOR == '/') {
    //the site is hosted in linux server
    define('PAGE_FILE', __FILE__);
} else {
    //the site is hosted in Windows server, so some extra processing is necessary
    define('PAGE_FILE', str_replace(DIRECTORY_SEPARATOR, '/', __FILE__));
}
define('SITE_ROOT', substr(PAGE_FILE, 0, -(mb_strlen($_SERVER['PHP_SELF']))));

Now, simply write the code bellow to include your file, no matter where it is located:

include SITE_ROOT . '/includes/class/class.product.php'
include SITE_ROOT . '/my-path/another-file.php'
include SITE_ROOT . '/file-in-root-directory.php'

I hope this will give you a permanent solution.

user3221512
  • 206
  • 3
  • 5