php文件下载

function get_file_mime_type($filename){
  $mime = '';
  if (is_file($filename)) {
    $finfo = finfo_open(FILEINFO_MIME_TYPE); 
    $mime = finfo_file($finfo, $filename);
    finfo_close($finfo);
  }
  return $mime;
}

function download($filename){
  if (is_file($filename)) {
    header('Content-type:'. get_file_mime_type($filename));
    $basename = basename($filename);
    header("Content-Disposition:attachment;filename={$basename}");
    header('Content-Length: ' . filesize($filename));
    readfile($filename);
  } else {
    die('文件不存在或不是文件!');
  }
}

download('./code.txt');

你可能感兴趣的:(php文件下载)