Skip to content Skip to sidebar Skip to footer

Display Files In Directory Using Php And Jquery

I am trying to use PHP and jQuery to display files in a directory. I have incorporated Datatables to list all records in a database. Some of these records have files linked to the

Solution 1:

When trying to return json array from pure php script you could also set the header of the response

header('Content-type: application/json');
echo json_encode($ff);

But I would also populate the array first and return whole result in the end.

Solution 2:

Try this

$partnerCode = $_POST['editpartnercode'];
$dir = "D:/CargoDocsPDFs/" . $partnerCode;
$ffs = array_diff(scandir($dir), array('..', '.'));
echo json_encode($ffs);

Solution 3:

I was able to solve my problem by by altering the JQuery to look like this:

$.post('process/displayFiles.php', {editpartnercode:editpartnercode}, function(data)
{
    $('#allFiles').html(data);      
});

On the PHP side, I removed the json_encode and just echoed the list items:

foreach($ffsas$ff)
{               
  if($ff != '.' && $ff != '..')
  { 
    echo"<li><a href='".$path."' download target='_blank'>".$ff."<a></li>";
  }
}  

Using the above, I am now able to see the files in a modal window as links. I will have a part 2 to this question.

Post a Comment for "Display Files In Directory Using Php And Jquery"