2014-02-12 98 views
0

我想創建一個系統,該文件夾將包含文件夾中的任何.php文件,類似於wordpress\plugins文件夾。最好拖放。從文件夾中自動生成PHP

任何想法如何做到這一點?

+1

http://php.net/glob,http://php.net/foreach –

回答

1

問題不明確......

  • 讀取該文件夾的文件
  • 滾動通過的文件中發現
  • 如果PHP - 包括它
+0

我只想包括它,如果它的PHP。那就是所有 –

+0

這就是我的建議:「如果PHP - 包含它」。當你從文件夾中讀取文件時 - 文件名將有擴展名。檢查文件名。如果最後3個字符是'php' - 包含文件 – cyadvert

2
<?php 
function scandir2($dir) 
{ 
    $out = array(); 
    if (is_dir($dir)) 
    { 
     if ($dh = opendir($dir)) 
     { 
      while (($file = readdir($dh)) !== false) 
      { 
       if ($file == '.' or $file == '..') continue; 

       if (!is_dir($dir . '/'. $file)) 
       { 
        $out[] = $dir . '/' . $file; 
       } 
      } 
      closedir($dh); 
     } 
    } 
    sort($out); 
    return $out; 
} 

$i=scandir2("."); 
foreach($i as $name) 
{ 
    if(strstr($name ,'.php')) 
    include($name); 
} 
?> 

這個代碼掃描目錄幷包括php文件...

0

下面是代碼這樣做:

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

的​​3210函數返回指定路徑上的所有文件.php的數組。

0

這裏是我會怎麼做?

<?php 

$dirPath = '/path/to/files'; 
if ($handle = opendir($dirPath)) { 

    /* loop over files in directory */ 
    while (false !== ($entry = readdir($handle))) {   

     /* find extension */ 
     $ext = substr($entry,-3); 
     $ext = strtolower($ext); 

     /* include file if extension is PHP */ 
     if ($ext === 'php') { 
      include_once($dirPath .'/'. $entry); 
     } //if 

    } //if 


    closedir($handle); 
} //if 

注意,有一些安全問題和可能的性能問題。