Append index to others to files in the folder in general and within subfolders files

0

When using <? php include("index.php"); ?> it works but only in the files that are in the main folder, where this index and the other files, but when I want to link the index to the files inside the folders, for example within the exercise folder gives me an error and says that the file does not exist inside that folder, what can I do in that case?

    
asked by Andres Gonzalez 28.02.2017 в 03:34
source

2 answers

0

If the index.php is in the root of your project, the subfolders should call it ../index.php . If it is the subfolder of a folder, then it would be ../../index.php and so on. If the prefix ../ in the path means "upload a directory" then you must apply it as many times as depth levels of each file.

- raíz del proyecto (require 'index.php')
  - ejemplo2 (require '../index.php')
    - ejemplos2_corregidos (require '../..index.php')
  - guia2 (require '../index.php')

This method is practicable if you have few directories and few files. As a project grows it is easier to have few entrypoints and have them define a constant by setting the fixed index route for all the other scripts that are loaded in a request. For example, in a file _bootstrap.php include:

<?php

  define('HOMEDIR',__DIR__);

And that way, in any other script that is included, you would call index.php like:

<?php

  require(HOMEDIR.'/index.php');

For this to work, you would have to make every visit start from the same entrypoint that in turn redirected it to the script that you were really looking for using a parameter. In your case, the same index.php could be that entrypoint.

    
answered by 28.02.2017 / 11:40
source
-1

an example for the "guide" folder would be "../guia2/index.php" and so it would be every time you want to call it, of course the route changes depending on where you look for it

    
answered by 28.02.2017 в 06:02