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

Asked

Viewed 19,501 times

3

I’m taking 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 far so good, only when I call the class fitaDao, he can’t find his way to class Conexao.

<?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(../../connected/Connected.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 '.. /.. /conexao/Conexao.php' (include_path='C: xampp php PEAR') in C: xampp htdocs vintagelocadora app model dao Fitadao.php on line 2

Here’s a screenshot of my directory:
inserir a descrição da imagem aqui

2 answers

5


I recommend to "avoid conflicts" that you create in your index.php a constant that should have the complete 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 that all files are called by index.php, so you don’t 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 on 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;
  }

}
?>
  • 1

    vlw guy was that same, thank you very much

  • @Rodrigojacinto It’s okay to want ../../, but trust me, it will cause you more difficulties of the similar.

  • 1

    @Rodrigojacinto I find the solution presented by Guilherme more appropriate. My answer solves the problem directly but raises another question that may arise in the future if your class is reused, but okay, you choose the most appropriate answer to your problem.

  • @Guilhermenascimento then answer the god less headache Felipe to fix the code, but I see that yours will solve my future problems tbm, thank you

  • @Rodrigojacinto excuse, can mark his, but the question of headache and goes from the limitation of knowledge (how much you already know) x understanding (what you understood from my explanation), this answer questionss shall assist you http://answall.com/q/60455/3635

  • @Guilhermenascimento All right thank you

Show 1 more comment

1

The archive FitaController.php does the file include FitaDao.php which in turn does include the file Conexao.php, however the path must be relative to the first include, ie in relation to the FitaController.php.

In your file FitaDao.php change:

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

For:

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

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

One of the solutions would be to use autoload.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.