<?php
/**
* User: yuzhao
* CreateTime: 2018/10/30 上午11:58
* Description:
*/
class FileTool {
/**
* User: yuzhao
* CreateTime: 2018/10/30 下午12:22
* @var
* Description: 文件夾相對路徑
*/
private $path;
/**
* FileTool constructor.
*/
public function __construct($path='')
{
$this->path = $path;
}
/**
* User: yuzhao
* CreateTime: 2018/10/30 下午12:21
* @param $path
* Description: 返回當前對象
*/
public static function instance($path='') {
return new FileTool($path);
}
/**
* User: yuzhao
* CreateTime: 2018/10/30 下午12:18
* @param $dir
* Description: 加載目錄下的所有文件
*/
public function includeAll($dir){
foreach ($dir as $key => $value) {
$value = $this->path.$value;
//打開文件夾
$handler = opendir($value);
//遍歷腳本文件夾下的所有文件
while( (($filename = readdir($handler)) !== false) ){
//如果文件為php腳本文件
if( substr($filename,-4) === '.php' ){
if ($filename == 'FileTool.php') {
continue;
}
//將文件包含進來
require_once( $value.'/'.$filename );
print_r($value.'/'.$filename."\n");
}
}
//關(guān)閉文件夾
closedir($handler);
}
}
}
使用樣例
// 將自動導出數(shù)據(jù)api文件加載進來
FileTool::instance('需要從哪里開始加載')->includeAll(array(
'/子目錄1','/子目錄2
));