PHP failed to open stream when trying to include top folder

Asked

Viewed 954 times

0

I have a problem in my PHP that I do not know what is happening.

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

I don’t know why this is happening because the correct way to get back folders would be ../javascript/jquery.js and this is getting in the way of some processes. When I use include include_once("./model/Produto.php") and when I’m gonna call that include somewhere else it gives the bug 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 which takes what was typed in the url and gives a include_once("caminho/arquivo.php"), this can influence something?

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

2 answers

1

Suppose the following folder hierarchy:

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

If you’re inside the file arquivo1.php and want to make a require for the archive index.php Voce must do require_once '../index.php'.

If you want to make one require_once from inside the archive index.php to the arquivo1.php you must do require_once './pasta1/arquivo1.php'.

This organization will fail if there are multiple inclusions, such as file 1.php including index.php and index.php including file 2.php.

1

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

The file directory. If used within a include, the directory of the included file is returned. It is equivalent to dirname(FILE). The directory name has no bar at the end, unless the root directory.

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

In the case of a structure:

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

Inside controller1.php if you want to include the model1.php file would be:

include(__DIR__ . "/../model/model1.php");
  • The problem is that using DIR I can’t get back folders

  • Warning: include_once(C: xampp htdocs Projetobusca view../controller/Empresacontrolador.php): failed to open stream

  • Yes, I’m sorry... I forgot the bar at the end.

Browser other questions tagged

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