php - 为目录树动态创建下载链接

标签 php javascript jquery codeigniter

我正在尝试使用 Codeigniter 作为平台,基于文件夹结构和其中的各种文件创建目录树。我的 View 工作正常(除了下载指定文件的文件链接。)我对 Jquery 和 java 很陌生我的代码基于 Interwebs 上提到能够添加下载链接但没有解释如何添加的内容。

<html>
<?PHP if ($_SESSION['profilepath'] != NULL) { ?>
<div id="files">
<?php //print_r($folders);?>
</div>
<script type="text/javascript">
$(document).ready(function() {
var files = <?php print_r(json_encode($folders)); ?>;
var file_tree = build_file_tree(files);
file_tree.appendTo('#files');

function build_file_tree(files) {

    var tree = $('<ul>');

    for (x in files) {

        if (typeof files[x] == "object") {
            var span = $('<span>').html(x).appendTo(
                $('<li>').appendTo(tree).addClass('folder')
            );
            var subtree = build_file_tree(files[x]).hide();
            span.after(subtree);
            span.click(function() {
                $(this).parent().find('ul:first').toggle();
            });

        } else {
            $('<li>').html(files[x]).appendTo(tree).addClass('file').click(function(){
                window.location=$(this).find("a").attr("href");return false;})
           //The click() function in the line above is where my links for download should be but I am unsure of what to do from here.

        }

    }

    return tree;

}
});    
</script>
</head>
<body>
<?PHP
} else {
$error = "Your user path is not set.";
  print_r($error);
}
?>
</body>
</html>

最佳答案

嗯,我认为它更像是一个 PHP 的东西?您设置指向您的 php 脚本的链接,将文件名作为变量传递

href="download.php?file=folder\folder\folder\filename.ext"(可能需要对其进行哈希处理/url 编码)

$file = (isset($_GET['file']))?$_GET['file']:"";

检查它是否一切正常等..然后强制它显示下载对话框

if (!is_readable($file)) die('File not found or inaccessible!');

$size = filesize($file);
$name = rawurldecode($filename);
$known_mime_types = array(
    "pdf"  => "application/pdf",
    "txt"  => "text/plain",
    "html" => "text/html",
    "htm"  => "text/html",
    "exe"  => "application/octet-stream",
    "zip"  => "application/zip",
    "doc"  => "application/msword",
    "docx" => "application/msword",
    "xls"  => "application/vnd.ms-excel",
    "xlsx" => "application/vnd.ms-excel",
    "ppt"  => "application/vnd.ms-powerpoint",
    "gif"  => "image/gif",
    "png"  => "image/png",
    "jpeg" => "image/jpg",
    "jpg"  => "image/jpg",
    "php"  => "text/plain"
);

$file_extension = strtolower(substr(strrchr($file, "."), 1));
if (array_key_exists($file_extension, $known_mime_types)) {
    $mime_type = $known_mime_types[$file_extension];
} else {
    $mime_type = "application/force-download";
}


@ob_end_clean(); //turn off output buffering to decrease cpu usage

// required for IE, otherwise Content-Disposition may be ignored
if (ini_get('zlib.output_compression')) ini_set('zlib.output_compression', 'Off');

header('Content-Type: ' . $mime_type);
header("Content-Transfer-Encoding: binary");
header('Accept-Ranges: bytes');

/* The three lines below basically make the
            download non-cacheable */
header("Cache-control: private");
header('Pragma: private');
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");

header("Content-Length: " . $size);

$new_length = $size;

$chunksize = 1 * (1024 * 1024); //you may want to change this
$bytes_send = 0;
/* output the file itself */
$chunksize = 1 * (1024 * 1024); //you may want to change this
$bytes_send = 0;
if ($file = fopen($file, 'r')) {
    if (isset($_SERVER['HTTP_RANGE'])) fseek($file, $range);

    while (!feof($file) && (!connection_aborted()) && ($bytes_send < $new_length)) {
        $buffer = fread($file, $chunksize);
        print($buffer); //echo($buffer); // is also possible
        flush();
        $bytes_send += strlen($buffer);
    }
    fclose($file);
} else die('Error - can not open file.');

关于php - 为目录树动态创建下载链接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6311488/

相关文章:

php - 如何获取公共(public)布局文件的数据并显示在所有 View 上

php - 实时php ajax在mysql数据库中编辑阿拉伯语单词给我问号字符(???)

javascript - phonegap admob 插件在 xcode 中获取横幅高度

javascript - 轮播效果 - 使元素在第一次迭代后消失

javascript - 滑动到网站中的不同页面

php - 排列、跳过和 URL 重写

php - 根据计数更新一组记录 - MySQL

javascript - 另一个里面的 ui-view 不显示

javascript - 选择框选项没有被 jquery 使用 ajax 响应数据选择

jquery - iFrame 页面上的 iframe-resizer 更改已重新加载