PHP include does not recognize XML file

1

I have a php project in a structured main folder as follows:

  • Container folder:
    • Folder config
      • XML file conexion.xml
    • Folder php
      • sub-folder bd
        • PHP file conexionbd.php
      • PHP file consultas.PHP
    • HTML file index.html

conexion.xml

<?xml version="1.0"?>
<conexion>
<servidor>localhost</servidor>
<usuario>
        <usuario_bd>usuario</usuario_bd>
        <contrasena_bd>contrasena</contrasena_bd>
</usuario>
<bd>nombre_bd</bd>
</conexion>

conexionbd.php

<?php
if (file_exists("../../config/conexion.xml")) {

$archivo_xml= simplexml_load_file("../../config/conexion.xml");

/*Se asigna los valores del archivo XML a las variables PHP*/
$servidor=$archivo_xml->servidor;
$usuario_bd=$archivo_xml->usuario->usuario_bd;
$contrasena_bd=$archivo_xml->usuario->contrasena_bd;
$bd=$archivo_xml->bd;

try{

    $conn= new mysqli($servidor,$usuario_bd,$contrasena_bd,$bd);
    echo "Conectado";
}catch(Exception $e){
    echo $e;
}


}else{
echo "el archivo no existe";
}

?>

If I run the application until here the result is the following:

consultas.php

<?php
include("bd/conexionbd.php");
?>

At the time of running the application here it shows me the following:

Why is it that the include("bd/conexionbd.php") does not detect the XML file and how can I fix it?

    
asked by Ferny Cortez 06.11.2017 в 20:48
source

3 answers

1

The problem is context; on the way in the statement of the function file_exists in the file conexionbd.php .

The relative path used there is valid when navigating directly to that file, but when navigating to consultas.php , the first one is executed indirectly, meaning that when the execution sequence reaches the first line of conexionbd.php , the path specified in a relative way as you put in the code "../../config/conexion.xml" is not valid, because at that moment the valid one would be "../config/conexion.xml" .

One solution would be to put this last path, but it has the disadvantage that you can not run it directly (which should not be necessary in your application anyway) and that you can only include it and work correctly from a file that is in a position in your folder structure such that the specified path is valid.

The solution that I would give you without changing too much the scheme you have implemented is to use a global variable that must be specified by each file declaring the path that corresponds to it.

conexionbd.php

<?php
if (file_exists("$PROJECTROOT/config/conexion.xml")) {

$archivo_xml= simplexml_load_file("$PROJECTROOT/config/conexion.xml");

/*Se asigna los valores del archivo XML a las variables PHP*/
$servidor=$archivo_xml->servidor;
$usuario_bd=$archivo_xml->usuario->usuario_bd;
$contrasena_bd=$archivo_xml->usuario->contrasena_bd;
$bd=$archivo_xml->bd;

try{

    $conn= new mysqli($servidor,$usuario_bd,$contrasena_bd,$bd);
    echo "Conectado";
}catch(Exception $e){
    echo $e;
}    
}else{
echo "el archivo no existe";
}

?>

consultas.php

<?php
  $PROJECTROOT = "../";
  include("bd/conexionbd.php");
?>
    
answered by 06.11.2017 / 21:28
source
0
  

The final solution is as follows (Using all the answers):

conexionbd.php

<?php
if (file_exists("../config/conexion.xml")) {

$archivo_xml= simplexml_load_file("../config/conexion.xml");

/*Se asigna los valores del archivo XML a las variables PHP*/
$servidor=$archivo_xml->servidor;
$usuario_bd=$archivo_xml->usuario->usuario_bd;
$contrasena_bd=$archivo_xml->usuario->contrasena_bd;
$bd=$archivo_xml->bd;

try{

    $conn= new mysqli($servidor,$usuario_bd,$contrasena_bd,$bd);
    echo "Conectado";
}catch(Exception $e){
    echo $e;
}


}else{
echo "el archivo no existe";
}

?>
    
answered by 06.11.2017 в 23:17
0

You may want to use vlucas / phpdotenv to save the connection data among others that your application uses. to store and use them safely.

    
answered by 07.11.2017 в 13:06