1

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.

Community
  • 1
  • 1
Debashis
  • 544
  • 2
  • 14
  • 33
  • You may include the file. If you want to *parse* the array, you might check this [question](http://stackoverflow.com/q/17118032/). There is a [custom php parser](http://stackoverflow.com/a/17134904/) and a [regex solution](http://stackoverflow.com/a/17134110/) in the answers – HamZa Jun 21 '13 at 12:31

6 Answers6

4

You don't need file_get_contents:

require_once 'config.php'
var_dump($CONFIG);
HamZa
  • 14,051
  • 11
  • 52
  • 72
Shuro
  • 153
  • 5
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.

Adam
  • 1,935
  • 2
  • 26
  • 55
3

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');
Karl Adler
  • 14,019
  • 10
  • 62
  • 86
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()

KAsh
  • 194
  • 4
  • 23
  • Yes, i am using codeigniter but the config.php file is located outside of the framework. I will try your suggestion. – Debashis Jun 21 '13 at 12:40
0

simple

include_file "config.php";
DevZer0
  • 13,316
  • 6
  • 25
  • 50
0

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

hek2mgl
  • 143,113
  • 25
  • 227
  • 253