Is there an easy way to require all files in a folder?
是否有一种简单的方法来要求文件夹中的所有文件?
8 个解决方案
#1
20
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;
}
}
#2
45
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()更有效。
#3
12
There is no easy way, as in Apache, where you can just Include /path/to/dir
, and all the files get included.
没有简单的方法,就像在Apache中一样,你可以只包含/ path / to / dir,并包含所有文件。
A possible way is to use the RecursiveDirectoryIterator from 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 .php
ending files from $path
, no matter how deep they are in the structure.
这将从$ path中提取所有.php结尾文件,无论它们在结构中有多深。
#4
9
Simple:
简单:
foreach(glob("path/to/my/dir/*.php") as $file){
require $file;
}
#5
2
Use a foreach
Loop.
foreach (glob("classes/*") as $filename) {
require $filename;
}
For more details, check out this previously posted question:
有关更多详细信息,请查看此前发布的问题:
#6
0
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;
}
#7
0
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);
}
#8
0
My way to require all siblings:
我要求所有兄弟姐妹的方式:
<?php
$files = glob(__DIR__ . '/*.php');
foreach ($files as $file) {
// prevents including file itself
if ($file != __FILE__) {
require($file);
}
}
#1
20
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;
}
}
#2
45
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()更有效。
#3
12
There is no easy way, as in Apache, where you can just Include /path/to/dir
, and all the files get included.
没有简单的方法,就像在Apache中一样,你可以只包含/ path / to / dir,并包含所有文件。
A possible way is to use the RecursiveDirectoryIterator from 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 .php
ending files from $path
, no matter how deep they are in the structure.
这将从$ path中提取所有.php结尾文件,无论它们在结构中有多深。
#4
9
Simple:
简单:
foreach(glob("path/to/my/dir/*.php") as $file){
require $file;
}
#5
2
Use a foreach
Loop.
foreach (glob("classes/*") as $filename) {
require $filename;
}
For more details, check out this previously posted question:
有关更多详细信息,请查看此前发布的问题:
#6
0
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;
}
#7
0
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);
}
#8
0
My way to require all siblings:
我要求所有兄弟姐妹的方式:
<?php
$files = glob(__DIR__ . '/*.php');
foreach ($files as $file) {
// prevents including file itself
if ($file != __FILE__) {
require($file);
}
}