2

I want to get files list (from a folder named uploads.

get_files.php

<?php
$dir = 'uploads/';
$a = scandir($dir);
$b=count($a);

for($x=2;$x<$b;$x++)
   {
   echo"<div class='filePass'>";
     echo $a[$x];
     echo "</div>";
   }
?>

get_files.php works well as alone, i.e. it properly lists the files.
But how can I get the list inside insideT div ?
I can include get-files.php but I need to do this using jquery ajax, because of reusing the function later.

function get_files(){
   $.ajax({
    url : 'get_files.php',
    type : 'get'
    });
    $('#insideT').html(//here I want to get the file listing);
    }
get_files();
qadenza
  • 9,025
  • 18
  • 73
  • 126

2 Answers2

6

Try this

get_files.php

<?php
$dir = 'uploads/';
$a = scandir($dir);
$b=count($a);
$res = '';
for($x=2;$x<$b;$x++)
   {
     $res.= "<div class='filePass'>";
     $res.= $a[$x];
     $res.= "</div>";
   }
echo $res;
?>

Ajax script

function get_files(){
    $.ajax({
               type: "GET",
               url: "get_files.php",
               cache: false,
               success: function(result){
                     $("#insideT").html(result);
               }
          });
}
user2943773
  • 254
  • 1
  • 4
  • 10
-1

scandir is bad-practice :(

Use faster solution: https://stackoverflow.com/a/30949072/257319

Community
  • 1
  • 1