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
___ erkimt ___ error: "failed to open stream: No such file or directory" ______ qstntxt ___

I'm getting the data from a form screen.

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

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

...

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

<?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

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:

    
______ ___ azszpr180848

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

index.php:

<?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;
  }

}
?>

I believe that all files are called by index.php, so no need to do anything but this:

In FitaDao.php do so:

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

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

...

And in the form:

<?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
    
______ azszpr180852 ___

The file %code% makes the include of the file %code% which in turn makes the file %code% include, however the path must be relative to the first include, that is, relative to %code% . / p>

In your %code% file switch:

<?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;
  }

}
?>

By:

%pre%

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 %code% directory.

One solution would be to use autoload .

    
___
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