2017-09-11 56 views
0
$('#lang_choice1').each(function() { 
        var lang = $(this).val(); 
        $('#lang_files').empty(); 
        $.ajax({ 
         type: "POST", 
         url: '<?= site_url('translation/language/getDirContents') ?>', 
         data: {"lang":lang}, 
         dataType:"json", 
         success: function (data) { 
          $.each(data,function(id,dir) //here we're doing a foreach loop round each directory with id as the key and directory as the value 
          { 
           var opt = $('<option />'); // here we're creating a new select option with for each directory 
           opt.val(dir); 
           opt.text(dir.substr(dir.lastIndexOf("\\")+1)); 
           $('#lang_files').append(opt); //here we will append these new select options to a dropdown with the id 
          }); 
          $("#lang_files").val($("#lang_files option:first").val()); 
         } 

        }); 
        return false; 
       }); 

opt.text(dir.substr(dir.lastIndexOf("\\")+1));的用法在UNIX system.But在窗戶導致問題的正常工作。在UNIX中,它需要是「/」我如何使它兼容在Windows和基於Unix的系統中使用?問題「//」在UNIX

+1

參見[RecursiveDirectoryIterator(https://secure.php.net/manual/en/class.recursivedirectoryiterator.php)。 –

+0

幾乎是https://stackoverflow.com/questions/14304935/php-listing-all-directories-and-sub-directories-recursively-in-drop-down-menu的副本。那裏的答案應該只需稍作修改即可獲得文件而不是目錄。 –

回答

0

我用這個風格:

$dir = "/root/"; // Root directory to start at 

function scanServer($dir){ 
    global $totalList; 
    global $fileList; 
    global $dirList; 
    global $pathList; 

    $files = scandir($dir);    // Obtain all files into an array 
    foreach($files as $file){ 

     if($file != '.' && $file != '..'){ 
     if(is_dir($dir.'/'.$file)){  // Run self if it is a directory 
      $dirList[] = $file;    // Creates a master list of all directories 
      scanServer($dir.'/'.$file); 
     }else{ 
      $fileList[] = $file;   // Creates a master list of all files 
      $pathList[] = $dir.'/'.$file; // Creates a master list of absolute paths 

     } 
     } 
    } 
    } 

foreach($fileList as file){ 

    echo "<option value='".$file.">".$file."/>"; 
}