Include All Files in a Directory

How to include() all PHP files from a directory?

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

Include all files in a directory?

In Bash:

HEADER=all_headers.h
echo "#ifndef __ALL_HEADERS__" > $HEADER
echo "#define __ALL_HEADERS__" >> $HEADER
for file in dir/*.h
do
echo "#include <$file>" >> $HEADER
done
echo "#endif" >> $HEADER

How to include all PHP files in directory?

I always do that like this where you would put your include normally but then in a foreach:

foreach(glob('dir/*.php') as $file) {
include_once $file;
}

that is maybe not the best way, it is always a good idea to create a list maybe an array of filepaths and then put that in the foreach like:

$includes = array(
'path/to/file.php',
'path/to/another/file.php'
);

foreach($includes as $file) {
include_once $file;
}

then whenever you add a file you can add one to that list and it will be included

Require all files in a folder

No short way of doing it, you'll need to implement it in PHP. Something like this should suffice:

foreach (scandir(dirname(__FILE__)) as $filename) {
$path = dirname(__FILE__) . '/' . $filename;
if (is_file($path)) {
require $path;
}
}

SSI Include all files in directory?

There’s no SSI instruction doing what you’re asking
https://en.wikipedia.org/wiki/Server_Side_Includes

You might consider exec which is supported by Apache if running an external process on every request is a viable solution for you (I woudn't recommend that)

The exec command executes a given shell command or CGI script

So you just need to feed it a bash command or external scripts that lists all the files in the directory

But exec is not supporteed in nginx, here's the discussion
and documentation

I don’t know what exactly you’re trying to solve, but I would approach it differently - either use
ssi include + url subrequest to the backend doing whatever you need or concatenate the files into a single bundle[s] and handle them using a regular ssi include + a local file path

How can I get the list of files in a directory using C or C++?

UPDATE 2017:

In C++17 there is now an official way to list files of your file system: std::filesystem. There is an excellent answer from Shreevardhan below with this source code:

#include <string>
#include <iostream>
#include <filesystem>
namespace fs = std::filesystem;

int main()
{
std::string path = "/path/to/directory";
for (const auto & entry : fs::directory_iterator(path))
std::cout << entry.path() << std::endl;
}

Old Answer:

In small and simple tasks I do not use boost, I use dirent.h. It is available as a standard header in UNIX, and also available for Windows via a compatibility layer created by Toni Ronkko.

DIR *dir;
struct dirent *ent;
if ((dir = opendir ("c:\\src\\")) != NULL) {
/* print all the files and directories within directory */
while ((ent = readdir (dir)) != NULL) {
printf ("%s\n", ent->d_name);
}
closedir (dir);
} else {
/* could not open directory */
perror ("");
return EXIT_FAILURE;
}

It is just a small header file and does most of the simple stuff you need without using a big template-based approach like boost (no offence, I like boost!).

Include all files in a folder - PHP

PHP's include shouldn't be used for other file types, like .json. To extract data from those files you'll want to read them using something like file_get_contents. For example:

$data = json_decode(file_get_contents('someFile3.json'));

To recursively include the PHP files in other directories you can try recursively searching through all directories:

function require_all($dir, $max_scan_depth, $depth=0) {
if ($depth > $max_scan_depth) {
return;
}

// require all php files
$scan = glob("$dir/*");
foreach ($scan as $path) {
if (preg_match('/\.php$/', $path)) {
require_once $path;
}
elseif (is_dir($path)) {
require_all($path, $max_scan_depth, $depth+1);
}
}
}

$max_depth = 255;
require_all('folder3', $max_depth);

This code is a modified version of the code found here: https://gist.github.com/pwenzel/3438784



Related Topics



Leave a reply



Submit