PHP – How Do You Load all PHP files in a directory?

0
235
Asked By Elli Mongillo On

Rather than having to use an include a statement to include every new PHP file I add to a directory, is there a way to tell PHP to just load all files in a directory. Some kind of recursive call to load everything.

1 Answer

Answered By Chris Evans On

There is no recursive version of an include statement but it is quite simple to do this using code. I would discourage this sort of thing though. If you dropped a file in that had an error, it would break the entire script as there would be no way to only include this file if the code was running in debug mode or something. The code below will do what you are looking to do but use it with caution as this automation comes with some risks to it. You might save having to copy and paste a line of code but it makes debugging code a lot harder and risks bugs being introduced simply from dropping a file in a directory. You might want to use some better error handling to ensure that a file with some syntax errors is not included.

foreach (glob(dirname(__FILE__) ."/foldername/*.php") as $filename) 
{ 
    include $filename; 
}

 

Related Questions

Convert Json To Xml

Bitrate Converter

GUID Generator

GUID Validator

Convert Json To C# Class

LEAVE A REPLY

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.