Đây là giải pháp đơn giản để liệt kê tất cả các tệp trong thư mục và tải xuống tệp.
Trong file index.php của bạn
<?php
$dir = "./";
$allFiles = scandir($dir);
$files = array_diff($allFiles, array('.', '..')); // To remove . and ..
foreach($files as $file){
echo "<a href='download.php?file=".$file."'>".$file."</a><br>";
}
Chức năng scandir() danh sách tất cả các file và thư mục bên trong đường dẫn cụ thể. Nó hoạt động với cả hai PHP 5 và PHP 7.
Bây giờ trong download.php
<?php
$filename = basename($_GET['file']);
// Specify file path.
$path = ''; // '/uplods/'
$download_file = $path.$filename;
if(!empty($filename)){
// Check file is exists on given path.
if(file_exists($download_file))
{
header('Content-Disposition: attachment; filename=' . $filename);
readfile($download_file);
exit;
}
else
{
echo 'File does not exists on given path';
}
}
Nguồn
2017-05-22 05:48:19
Đó không phải là xử lý bởi các trình duyệt? – SomeKittens
nhưng làm cách nào để trình duyệt bắt đầu tải xuống? –
Cung cấp liên kết đến tệp? – Nick