Bir klasördeki tüm dosyaları gerektiren nasıl?

6 Cevap

Bir klasördeki tüm dosyaları gerektiren kolay bir yolu var mı?

teşekkürler

6 Cevap

Bunu yapmanın kısa yol, PHP bunu uygulamak gerekir. Böyle bir şey yeterli olacaktır:

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

Muhtemelen sadece böyle bir şey yaparak:

$files = glob($dir . '/*.php');

foreach ($files as $file) {
    require($file);   
}

Bu glob() 'den opendir() ve readdir() kullanmak daha verimli olabilir.

There is no easy way, as in Apache, where you can just ''Include /path/to/dir'', and all the files get included.

Olası bir yolu SPL RecursiveDirectoryIterator kullanmaktır:

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());
        }
    }
}

Bu olursa olsun yapısında ne kadar derin $ yolundan, dosyaları biten tüm. Php çeker.

Basit:

foreach(glob("path/to/my/dir/*.php") as $file){
    require $file;
}

Require_all () fonksiyonu olarak:

//require all php files from a folder
function require_all ($path) {

    foreach (glob($path.'*.php') as $filename) require_once $filename;

}