Question

I'm racking my brain here and after several attempts and research I was forced to ask for assistance from you.

I have two files as follows:

- SITE/conf1g.php 
- SITE / library / Mysqli.class.php 

The conf1g.php file contains all site settings file and Mysqli is what makes the connection to the database using the variables defined in the file conf1g.php

But I use the Mysqli.class.php at multiple locations using require_once. How do I include the file in conf1g.php __ construct the class? (The class is in Mysqli.class.php file)

The best I could get was this:

<?php
function __construct() {

 $path = $_SERVER['SCRIPT_FILENAME'];
 $pa = $path_parts = pathinfo($path);
 $file = str_replace($path_parts['basename'],'',$_SERVER['SCRIPT_FILENAME']);
 $file = str_replace($file,'',dirname(__FILE__));
 $file = str_replace('library','conf1g.php',$file);
 require_once($file);

 //PDO CONNECTION CODE HERE

}
?>

But the error if I use subfolders like:

-/var/www/folder/sub_folder/userhere.php 

And the site is at:

-/var/www/SITE /

Any idea how to include this file in a dynamic way? Or any solution?

Was it helpful?

Solution

thanks for the help. Managed to make a half inelegant code here but it worked in all cases and had no problems.

    $path = $_SERVER['SCRIPT_FILENAME'];
    $pa = $path_parts = pathinfo($path);
    $file = str_replace($path_parts['basename'],'',$_SERVER['SCRIPT_FILENAME']);
    $file = str_replace($file,'',dirname(__FILE__));
    $file = str_replace('library','conf1g.php',$file);
    $file = str_replace($_SERVER['DOCUMENT_ROOT'],'',$file);

    $subcasas = explode('/',str_replace($_SERVER['DOCUMENT_ROOT'],'',dirname(__FILE__)));
    if(!is_file($file)){
        $file = substr( $file, 1 );
        for($x=0; $x<count($subcasas); $x++){
            $file = str_replace($subcasas [$x],'..',$file);

            if(is_file($file)){
                break; //que deselegante
            }
        }
    }
    $this->file_config = $file;
    require($this->file_config);
    $this->user = $configuracoes_bd_mysql[USER];
    $this->password = $configuracoes_bd_mysql[SENHA];
    $this->host = $configuracoes_bd_mysql[HOST];
    $this->db = $configuracoes_bd_mysql[DB];
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top