PHP failed to open stream when trying to include top folder

0

I have a problem with my PHP that I do not know what's going on.

Currently, whenever I go back a folder in some directory I use ./ for example: "./javascript/jquery.js"

I do not know why this is happening, because the correct way to get back folders would be ../javascript/jquery.js and this is messing up some processes. When I'm going to use include I also use include_once("./model/Produto.php") and when I call this include somewhere else it gives the error failed to open stream: No such file or directory because it is ./ and not ../

Is there any way to fix this? Remembering that whenever my index.php uses .htaccess in the middle of the page that uses a $_GET that takes what was typed in the url and gives include_once("caminho/arquivo.php") , can this influence something?

Edit1: if I use ../ to go back instead of ./ gives the failed to open stream: No such file or directory error in any directory

    
asked by anonymous 10.06.2017 / 15:46

2 answers

0

Suppose the following folder hierarchy:

raiz
  pasta1
    arquivo1.php
  pasta2
    arquivo2.php
  index.php

If you are inside the file arquivo1.php and you want to make a require for the file index.php you must require_once '../index.php' .

If you want to make a require_once from within the file index.php to arquivo1.php you should require_once './pasta1/arquivo1.php' .

This organization will fail if there are multiple inclusions, such as file1.php including index.php and index.php including file2.php.

    
10.06.2017 / 16:44
0

One possibility is to use the magic constant __DIR__ , according to the manual it contains:

  

The directory of the file. If used within an include, the directory of the   The included file is returned. It is equivalent to dirname ( FILE ). O   directory name does not have a slash at the end, unless it is the   root directory.

This way it is easier to locate yourself since the reference is always the path relative to the file you are editing.

In the case of a structure:

/index.php
   /model/model1.php
   /controller/controller1.php

Within controller1.php if you want to include the file model1.php would look like:

include(__DIR__ . "/../model/model1.php");
    
10.06.2017 / 16:54