Problem with the include

Asked

Viewed 107 times

-2

So I’m having trouble making the include. I have performed this same method several times to address the file that is in the controller folder, but now it is not working. What can be?

include_once $_SERVER['DOCUMENT_ROOT']."/controleEstoque/controller/MaterialController.class.php";
$cMaterial = new MaterialController();

inserir a descrição da imagem aqui inserir a descrição da imagem aqui

Problem solved. The problem is that I was just starting the tag with

  • Important you [Dit] your post and explain in detail the problem by describing what you tried and where is the current difficulty. Requests for mere code debug ready, tutorials and/or things involving more than one subject in the same post usually do not fit the site scope. Links to better understand how Sopt works: [Tour], [Ask], Manual on how NOT to ask questions and [Help]. If you are unsure of any particular detail, reducing the code to a [MCVE] can help a lot. Fundamental not posting text as image.

  • Okay, no problem. I was tested, I couldn’t find the answer. I’m new around here, so I’m learning, but thanks. :)

2 answers

1

Have you tried using the relative path? Maybe it works:

. = current directory

.. = previous directory

/ = root (systems *nix)

~ = home (also in *nix)

Or use the dirname().

ex:

require_once "{dirname(__FILE__)}/CaminhoRelativoDoArquivo.php";
require_once "{dirname(__FILE__)}/../ExemploArquivoDiretorioSuperior.php";
require_once "{dirname(__FILE__)}/../test/ExemploArquivoDiretorioVizinho.php";
require_once "{dirname(__FILE__)}/ExemploArquivoMesmoDiretorio.php";
  • 1

    So, even if we can accomplish these ways to get to the archive, it doesn’t work. It recognizes that the file exists, I can reach it through windows explorer, but does not find the class.

  • Ever tried to use an autoload? Then, instead of giving the includes, you just say what each class uses. You can even use two different classes with the same name in the same file.

0

The tip is before include you check if the file exists.

$file = $_SERVER['DOCUMENT_ROOT']."/controleEstoque/controller/MaterialController.class.php";
if(file_exists($file)){
    include $file;
}

You can also check which address is picking up, doing so:

echo $file;

Then correct the path as needed.

  • It continues with the same error. I liked this idea of making the check, I had never done, but even so, it doesn’t work. The path is correct, when I type, the printed path, in windows explorer it opens the file.

  • In this case, there may be something wrong with the creation of your class. Have to include the first line of the file "Materialcontroller.class.php" ??

Browser other questions tagged

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