wtw24
7/12/2018 - 11:38 AM

Как отдать на загрузку какой-либо файл на сервере с помощью php

Как отдать на загрузку какой-либо файл на сервере с помощью php

<?php

function download ($file) {
  if (!empty($file) and file_exists($file) and is_file($file)) {
    if (ob_get_level()) {
      ob_end_clean();
    }
 
    header('Content-Description: File Transfer');
    header('Content-Type: application/octet-stream');
    header('Content-Disposition: attachment; filename=' . basename($file));
    header('Content-Transfer-Encoding: binary');
    header('Expires: 0');
    header('Cache-Control: must-revalidate');
    header('Pragma: public');
    header('Content-Length: ' . filesize($file));
     
    if ($fd = fopen($file, 'rb')) {
      while (!feof($fd)) {
        print fread($fd, 1024);
      }
      fclose($fd);
    }
  } else {
    header('HTTP/1.0 404 Not Found');
    echo 'File not found';
  }
  exit();
}