3

I have the below mentioned code (actually, configuration) written in a file (config.php) and i want to get the content written in config.php in another file (check.php)

Codes written in config.php:

<?php
$CONFIG = array (
  'id' => 'asd5646asdas',
  'dbtype' => 'mysql',
  'version' => '5.0.12',
);

Code written in check.php to get the content is:

$config_file_path = $_SERVER['DOCUMENT_ROOT'] . 'config.php';
$config = file_get_contents($config_file_path);

Using the above code i am getting the output as a string and i wanted to convert it into an array. To do so, i have tried the below code.

$config = substr($config, 24, -5); // to remove the php starting tag and other un-neccesary things

$config = str_replace("'","",$config);
$config_array = explode("=>", $config);

Using the above code, i am getting an output like:

Array
(
    [0] =>   id 
    [1] =>  asd5646asdas,
  dbtype 
    [2] =>  mysql,
  version 
    [3] =>  5.0.12
)

which is incorrect.

Is there any way to convert it into an array. I have tried serialize() as well as mentioned in accessing array from another page in php , but did not succeed.

Any help on this will be appreciated.

1
  • You may include the file. If you want to parse the array, you might check this question. There is a custom php parser and a regex solution in the answers Commented Jun 21, 2013 at 12:31

6 Answers 6

8

You don't need file_get_contents:

require_once 'config.php'
var_dump($CONFIG);
Sign up to request clarification or add additional context in comments.

Comments

4

Use include or require to include and execute the file. The difference is just what happens when the file doesn't exist. inlcude will throw a warning, require an error. To make sure just to load (and execute) the file the first time you can also use include_once, require_one php.net If you are somehow not able to include the file (what reasons ever) take a look at the eval() function to execute php code from string. But this is not recommendet at all because of security reasons!

require_once('config.php');
include_once('config.php');
require('config.php');
include('config.php');

Comments

3

I'm very confused at your approach, if you just do this:

include(config.php);

Then your $CONFIG variable will be available on other pages.

print_r($CONFIG);

Take a look on the PHP website which documents the include function.

Comments

1

if you are using codeigniter no need to include the config file, you can access using $this, but if you are using plain php, u need to use include() or require() or include_once() or require_once()

1 Comment

Yes, i am using codeigniter but the config.php file is located outside of the framework. I will try your suggestion.
0

simple

include_file "config.php";

1 Comment

Oh, yes... I really forget this approach.
-1

How could you fail to read about the require_once directive? :)

$config = file_get_contents($config_file_path);

Must be:

require_once $config_file_path;
// now you can use variables, classes and function from this file
var_dump($config);

If you follow the link to the require_once manual page please read also about include and include_once. As a PHP developer you MUST know about this. It's elementary

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.