2011-06-10 68 views
2

我試圖創建一個基於文件夾結構和它使用Codeigniter作爲平臺的各種文件的目錄樹。我的視圖工作正常(文件的鏈接除了指定的文件以外)。我對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> 

回答

0

嗯,它更多的是一個PHP的東西,我覺得呢?你的鏈接設置爲你的PHP腳本傳遞文件名作爲變量

href="download.php?file=folder\folder\folder\filename.ext"(可能要散列它/ URL編碼)

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

檢查其一切OK等。然後迫使它顯示下載對話框

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.');