使用PHP和jQuery在目录中显示文件 [英] Display files in directory using PHP and jQuery

查看:182
本文介绍了使用PHP和jQuery在目录中显示文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用PHP和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 them. The user would click a link which opens a modal window which should then display all the files in that record's directory.

从PHP开始

<?php
if(isset($_POST['editpartnercode']))
{
    $partnerCode = $_POST['editpartnercode'];

    $dir = "D:/CargoDocsPDFs/" . $partnerCode;

    $ffs = scandir($dir);

    foreach($ffs as $ff)
    {               
        if($ff != '.' && $ff != '..')
        {   
            echo $ff;
            // echo json_encode($out); // <-this didn't work either
        }
    }       
}
?>

这是使用$ .post传递editpartnercode变量的jQuery:

Here is the jQuery that uses $.post to pass the editpartnercode variable:

$.post('process/displayFiles.php', {editpartnercode:editpartnercode}, function(data)
{
    console.log(data);
    var obj = JSON.parse(data);
    $('#allFiles').empty();
    var htmlToInsert = obj.map(function (item)
    {
        return '<li><b>' + item.ff + '</b></li>';
    }).join('');
    $('#allFiles').html(htmlToInsert);      
});

HTML看起来像这样:

The HTML looks like this:

<div class="row">
    <div class="col-lg-12">
        <p>Uploaded Files</p>
        <ul id="allFiles">
        </ul>
    </div>
</div>

我可以在控制台中看到文件名,但是我也遇到此错误:

I can see the file names in the console, but I am also getting this error:

VM2087:1 Uncaught SyntaxError: Unexpected token T in JSON at position 0
at JSON.parse (<anonymous>)
at Object.success (genhome.js:210)
at u (jquery.js:2)
at Object.fireWith [as resolveWith] (jquery.js:2)
at k (jquery.js:2)
at XMLHttpRequest.<anonymous> (jquery.js:2)

重申一下,我要做的就是列出与editpartnercode(这是目录名)关联的文件.

To reiterate, all I am trying to do is list the files associated with the editpartnercode (which is the directory name).

我缺少什么可以消除该错误并在屏幕上打印文件?

What am I missing that will get rid of that error and print the files on the screen?

这是在控制台中打印文件的方式:

Here is how the files are being printed in the console:

TEST1234567.TIFFTEST1234567_121218_100637.TIFFThumbs.db

推荐答案

我能够通过将JQuery更改为以下形式来解决我的问题:

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

在PHP方面,我删除了json_encode并回显了列表项:

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

foreach($ffs as $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.

这篇关于使用PHP和jQuery在目录中显示文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆