1. 程式人生 > >php+html下載檔案

php+html下載檔案

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>下載檔案</title>
    <style type="text/css">
              *{
                margin: 0 auto;
              }
              a{
                text-decoration: none;
                color: black;
              }
    </style>
</head>
<body>          

    <a href="downLog.php?filename=1.txt">下載 </a>    

</body>
</html>

/*

php檔案downLog.php

利用header屬性

*/

<?php

$fileName = $_GET['filename']; //得到檔名
$fileinfo = pathinfo($fileName);
header('Content-type: application/x-'.$fileinfo['extension']);
header('Content-Disposition: attachment; filename='.$fileinfo['basename']);
header('Content-Length: '.filesize($filename));
readfile($fileName

);  //讀取檔案內容
exit();

?>