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

2 Answers 2

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);
               }
          });
}
Sign up to request clarification or add additional context in comments.

Comments

-1

scandir is bad-practice :(

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.