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

The usage of opt.text(dir.substr(dir.lastIndexOf("\\")+1));is causing issue in UNIX system.But in windows it works fine. In UNIX it needs to be "/" How can I make it compatible to use in both windows and unix based systems?

Programmer
  • 157
  • 1
  • 14
  • 1
    See [RecursiveDirectoryIterator](https://secure.php.net/manual/en/class.recursivedirectoryiterator.php). – Alex Howansky Sep 11 '17 at 15:08
  • Almost a duplicate of https://stackoverflow.com/questions/14304935/php-listing-all-directories-and-sub-directories-recursively-in-drop-down-menu. The answer there should work with just a little modification to get files instead of dirs. – Don't Panic Sep 11 '17 at 15:11

1 Answers1

0

I use this style:

$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."/>";
}
clearshot66
  • 2,292
  • 1
  • 8
  • 17