error: "failed to open stream: No such file or directory"

1

I'm getting the data from a form screen.

<?php
include_once '../model/dao/FitaDao.php';
include_once '../model/vo/FitaVO.php';
include_once '../model/vo/FilmeVO.php';
include_once '../model/vo/CategoriaFilmeVO.php';
include_once '../model/vo/ArtistaVO.php';


class FitaController {

  public function insereFita(FitaVo $fita){
    $fd = new FitaDao();
    $fd->insereMidia($fita);
  }

}

  switch($_POST['acao']){

    case 1:{
      $categoria = new CategoriaFilmeVO($_POST['categoria']);
      $artista = new ArtistaVO($_POST['artista'], $_POST['data']);
      $filme = new FilmeVO($_POST['titulo'], $categoria);
      $filme->adicionaArtistas($artista);
      $fita = new FitaVO($_POST['formato'], $_POST['ano'], $filme);

      $fc = new FitaController();
      $fc->insereFita($fita);
      break;
  }

}
?>

So that's fine, but when I call the class fitaDao , it can not find the path of the Conexao class.

<?php
require_once "../../conexao/Conexao.php";
include_once '../vo/CategoriaFilmeVO.php';
include_once '../vo/ArtistaVO.php';
include_once '../vo/FilmeVO.php';
include_once '../vo/FitaVO.php';

class FitaDao {

  private $conexao;

  function __construct(){
    $conexao = new Conexao();
    $this->conexao = $conexao->conectar();
    $this->conexao->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING );
  }
?>

Generating the error:

  

Warning: require_once (../../ connection / Connection.php): failed to open stream: No such file or directory in C: \ xampp \ htdocs \ vintagelocadora \ app \ model \ dao \ FitaDao.php on line 2

     

Fatal error: require_once (): Failed opening required '../../connection/Conexao.php' (include_path = 'C: \ xampp \ php \ PEAR') in C: \ xampp \ htdocs \ vintagelocator \ app \ model \ dao \ FitaDao.php on line 2

Here's a picture of my directory:

    
asked by anonymous 30.01.2017 / 15:45

2 answers

3

I recommend to "avoid conflicts" that you create in your index.php a constant that must have the full path up to that point and this constant will be used in includes, like this:

index.php:

<?php
//usei o __FILE__ ao invés de __DIR__ devido a algunas questões de retrocompatibilidade

define('ROOT_PATH', dirname(__FILE__));

...

I believe all files are called by index.php, so you do not have to do anything else besides this:

In FitaDao.php do so:

<?php
require_once ROOT_PATH . "/conexao/Conexao.php";
include_once ROOT_PATH . '/model/vo/CategoriaFilmeVO.php';
include_once ROOT_PATH . '/model/vo/ArtistaVO.php';
include_once ROOT_PATH . '/model/vo/FilmeVO.php';
include_once ROOT_PATH . '/model/vo/FitaVO.php';

class FitaDao {

  private $conexao;

  function __construct(){
    $conexao = new Conexao();
    $this->conexao = $conexao->conectar();
    $this->conexao->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING );
  }
} //Faltava um } no final

And in the form:

<?php
include_once ROOT_PATH . '/model/dao/FitaDao.php';
include_once ROOT_PATH . '/model/vo/FitaVO.php';
include_once ROOT_PATH . '/model/vo/FilmeVO.php';
include_once ROOT_PATH . '/model/vo/CategoriaFilmeVO.php';
include_once ROOT_PATH . '/model/vo/ArtistaVO.php';

class FitaController {

  public function insereFita(FitaVo $fita){
    $fd = new FitaDao();
    $fd->insereMidia($fita);
  }

}

  switch($_POST['acao']){

    case 1:{
      $categoria = new CategoriaFilmeVO($_POST['categoria']);
      $artista = new ArtistaVO($_POST['artista'], $_POST['data']);
      $filme = new FilmeVO($_POST['titulo'], $categoria);
      $filme->adicionaArtistas($artista);
      $fita = new FitaVO($_POST['formato'], $_POST['ano'], $filme);

      $fc = new FitaController();
      $fc->insereFita($fita);
      break;
  }

}
?>
    
30.01.2017 / 15:58
0

The file FitaController.php makes the include of the file FitaDao.php which in turn makes the file Conexao.php include, however the path must be relative to the first include, that is, relative to FitaController.php . / p>

In your FitaDao.php file switch:

require_once "../../conexao/Conexao.php";

By:

require_once "../conexao/Conexao.php";

But this solution is a problem, if your class is included (thus reused) in other files in different directories, the same problem will occur if the directory is at a different level than the controller directory.

One solution would be to use autoload .

    
30.01.2017 / 16:03