-1

I am trying to run index.php file with include("folder_1")

+-- folder_1
|   +-- index.php
|   +-- other_stuff.php

In above mentioned directory structure. Normally to run index.php we'll write include("folder_1/index.php") . expecting to run the index.php file with include("folder_1") something like how import works in React.

Thanks :)

Umesh Jain
  • 13
  • 4
  • Do you want to include `index.php` only or _all files_ in that folder? – brombeer Apr 05 '22 at 09:08
  • I made the assumption that you wanted to include everything and answered accordingly. On a second read, I realize you may simply want to avoid saying `index.php`. If so, _there is no "default file to include from a directory" in PHP. If not, please update your post to clarify what exactly should be the outcome of your `include 'folder';` statement. – Markus AO Apr 05 '22 at 09:22
  • @brombeer I just want include index.php file not all the files if that's possible... – Umesh Jain Apr 05 '22 at 11:38
  • @MarkusAO Thanks! Actually, I was trying to find something related to default file to include – Umesh Jain Apr 05 '22 at 11:40
  • Right, there is no concept of "default file to include" in PHP. You'd have to make a helper function for it, e.g. `function import(string $dir) { include $dir . 'index.php'; }` if you wanted to emulate what React does. The caveats on variable scope apply, if you intend to import files with variables. Since my answer isn't to your actual question, I've ported it over to: [How to include() all PHP files from a directory?](https://stackoverflow.com/a/71751654/4630325). – Markus AO Apr 05 '22 at 12:22

3 Answers3

0

There is no concept of "default file to include" in PHP. However, you can easily create a helper function to accomplish this. (Whether this makes sense or not, I leave for you to decide.)

function import(string $dir) {
    include $dir . 'index.php'; 
} 

Note that if your file to import contains variables, they will be "lost" in the function's scope. If your file only defines classes, functions, constants etc. that are scope-independent, this will work fine. (Refer to my original answer for importing variables from files using a helper function.)


Edit: It turns out OP wasn't asking about including multiple files. My original answer has been ported over to: How to include() all PHP files from a directory?.

Markus AO
  • 4,771
  • 2
  • 18
  • 29
  • I can use this helper function when I am trying to include all files in the directory. But what I am trying to achieve is to run one file which will act as a start point of that particular directory. – Umesh Jain Apr 05 '22 at 12:12
  • @UmeshJain yes I realize that now. I'm updating this answer to reflect that. The original answer has been ported over to: [How to include() all PHP files from a directory?](https://stackoverflow.com/a/71751654/4630325). – Markus AO Apr 05 '22 at 12:23
  • I can work around with this function and also importing variables from files is great help! – Umesh Jain Apr 05 '22 at 13:59
  • Glad to hear. It should also be straight-forward enough to combine "import default file" with the `load_vars()` function from the original answer, if you had a use case for that. I find myself using the vars loading quite a bit, most commonly when loading extended configuration into classes -- for situations where said vars are only needed in particular situations, or would create unnecessary bloat in the class, or simply to keep them easily editable without touching the class itself (case in point being class-specific language strings). – Markus AO Apr 05 '22 at 15:41
  • I will keep that in mind! – Umesh Jain Apr 06 '22 at 11:42
0

this is not how the include method works, for doing that you need to write your custom method, can use something like that

foreach(glob('includes/*.php') as $file) {
   include($file);
}
Omar Tammam
  • 1,217
  • 1
  • 9
  • 17
0

You can only scan directory and include each file. There is no native function.

Example:

function include_dir($path) {
    
    $files = glob($path.'/*.php');
    
    if(count($files) > 0) {
        foreach($files as $file) {
            include $file;
        }
    }

}

include_dir("./app");