php - 如何使用 jquery ajax 获取文件列表

标签 php jquery ajax

我想获取文件列表(来自名为 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 单独工作效果很好,即它正确列出了文件。
但是如何获取 insideT div 内的列表?
我可以包含 get-files.php 但我需要使用 jquery ajax 来执行此操作,因为稍后会重用该函数。

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

最佳答案

试试这个

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 脚本

function get_files(){
    $.ajax({
               type: "GET",
               url: "get_files.php",
               cache: false,
               success: function(result){
                     $("#insideT").html(result);
               }
          });
}

关于php - 如何使用 jquery ajax 获取文件列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20784301/

相关文章:

permissions - 如果从浏览器调用 php exec 不执行

php - Amazon Elastic Beanstalk Worker cronjob (SQS) 多次触发同一消息

php - 带有 '+' 符号的 Jquery ajax 调用

php - 在 laravel 中创建文件夹

php - 我们可以从 php 文件传递​​一个包含 jquery $.get() 中 url 的变量吗

javascript - 如何在 Javascript 中获取相对路径?

php - 如何将所有这些 MySQL 查询合并为一个?

jquery - 基本航点使用

php - 自定义客户编辑页面: Upload PDF file and retrieve for display

javascript - div填充剩余高度