php 需要文件夹中的所有文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2692332/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Require all files in a folder
提问by never_had_a_name
Is there an easy way to require all files in a folder?
有没有一种简单的方法来要求文件夹中的所有文件?
采纳答案by soulmerge
No short way of doing it, you'll need to implement it in PHP. Something like this should suffice:
没有捷径可走,您需要在 PHP 中实现它。这样的事情应该足够了:
foreach (scandir(dirname(__FILE__)) as $filename) {
$path = dirname(__FILE__) . '/' . $filename;
if (is_file($path)) {
require $path;
}
}
回答by Tom Haigh
Probably only by doing something like this:
可能只有通过做这样的事情:
$files = glob($dir . '/*.php');
foreach ($files as $file) {
require($file);
}
It might be more efficient to use opendir()and readdir()than glob().
使用opendir()和可能readdir()比更有效glob()。
回答by K. Norbert
There is no easy way, as in Apache, where you can just Include /path/to/dir, and all the files get included.
没有简单的方法,就像在 Apache 中一样,您可以在其中仅Include /path/to/dir包含所有文件。
A possible way is to use the RecursiveDirectoryIteratorfrom the SPL:
一种可能的方法是使用SPL 中的RecursiveDirectoryIterator:
function includeDir($path) {
$dir = new RecursiveDirectoryIterator($path);
$iterator = new RecursiveIteratorIterator($dir);
foreach ($iterator as $file) {
$fname = $file->getFilename();
if (preg_match('%\.php$%', $fname)) {
include($file->getPathname());
}
}
}
This will pull all the .phpending files from $path, no matter how deep they are in the structure.
这将从 中提取所有.php结束文件$path,无论它们在结构中有多深。
回答by Tanner Ottinger
Simple:
简单的:
foreach(glob("path/to/my/dir/*.php") as $file){
require $file;
}
回答by Joshua Pinter
Use a foreachLoop.
使用foreach循环。
foreach (glob("classes/*") as $filename) {
require $filename;
}
For more details, check out this previously posted question:
有关更多详细信息,请查看之前发布的这个问题:
回答by Victor
As require_all() function:
作为 require_all() 函数:
//require all php files from a folder
function require_all ($path) {
foreach (glob($path.'*.php') as $filename) require_once $filename;
}
回答by Mirco Babin
Solution using opendir, readdir, closedir. This also includes subdirectories.
解决方案使用opendir、readdir、closedir。这也包括子目录。
<?php
function _require_all($path, $depth=0) {
$dirhandle = @opendir($path);
if ($dirhandle === false) return;
while (($file = readdir($dirhandle)) !== false) {
if ($file !== '.' && $file !== '..') {
$fullfile = $path . '/' . $file;
if (is_dir($fullfile)) {
_require_all($fullfile, $depth+1);
} else if (strlen($fullfile)>4 && substr($fullfile,-4) == '.php') {
require $fullfile;
}
}
}
closedir($dirhandle);
}
//Call like
_require_all(__DIR__ . '/../vendor/vlucas/phpdotenv/src');
回答by Raju Rajotia Jangid
recursively all file list and require_once in one directory:
递归地将所有文件列表和 require_once 放在一个目录中:
$files = array();
function require_once_dir($dir){
global $files;
$item = glob($dir);
foreach ($item as $filename) {
if(is_dir($filename)) {
require_once_dir($filename.'/'. "*");
}elseif(is_file($filename)){
$files[] = $filename;
}
}
}
$recursive_path = "path/to/dir";
require_once_dir($recursive_path. "/*");
for($f = 0; $f < count($files); $f++){
$file = $files[$f];
require_once($file);
}
回答by userlond
My way to require all siblings:
我要求所有兄弟姐妹的方式:
<?php
$files = glob(__DIR__ . '/*.php');
foreach ($files as $file) {
// prevents including file itself
if ($file != __FILE__) {
require($file);
}
}

