0

I'm using this code

$sort_data = get_sort('data/profiles/name.cms','¦');

The required "Data" folder is in the root of my host, but the php file that calling name.cms is in sub folder : styles/default/pages/left_member.php

and I'm failing to get that data from the name.cms file, any idea how to call the file from the root?

1 Answers1

1

You could make use of the PHP $_SERVER functionality and target the root directory.

Example:

$sort_data = get_sort($_SERVER['DOCUMENT_ROOT'].'/data/profiles/name.cms','¦');

It's important to note that what's actually defined as the root directory is defined in your configuration file.

'DOCUMENT_ROOT' - "The document root directory under which the current script is executing, as defined in the server's configuration file."

Source here.

UPDATE AS PER YOUR COMMENT:

Thank you for your help, I already tried it but it's getting whole xampp directory, so it didn't work.

If you ever take your project "live", the $_SERVER['DOCUMENT_ROOT'] functionality should work. The reason why it doesn't work for you right now, is because you have a "project structure" in your root directory. I.e. localhost/myproject/index.php. What you meant by root is actually the project folder and not the actual root folder.

In that case, you can try 3 different options.

  1. An absolute path without using the PHP reserved variable $_SERVER to find it.

Example:

$sort_data = get_sort('/data/profiles/name.cms','¦');
  1. Manually manipulate the directory path.

Example:

$sort_data = get_sort('../data/profiles/name.cms','¦');

You can add as many "level up", i.e. ../, as it takes to get to your desired starting point and then locate the folder.

  1. Define your own "root" path variable.

Example:

$my_root = $_SERVER['DOCUMENT_ROOT'].'/myproject';

You can now use $my_root with your path, like so:

$sort_data = get_sort($my_root.'/data/profiles/name.cms','¦');
Martin
  • 2,326
  • 1
  • 12
  • 22
  • Thank you for your help, I already tried it but it's getting whole xampp directory, so it didn't work. – Isaac Sabir Jun 22 '20 at 03:24
  • Then you're not going by root directory, but rather a project folder. i.e. *localhost/myproject/index.php*. If you ever go *"live"* with your project, the `$_SERVER['DOCUMENT_ROOT']` will work. Alternatively, you could backtrace your pathing. `../` means one folder back in the directory. Keep doing that until you can enter your specified path. – Martin Jun 22 '20 at 03:36
  • See edited answer for clearification on possible solutions @IsaacSabir – Martin Jun 22 '20 at 03:56