xref: /web-php/include/do-download.inc (revision a8c736a0)
1<?php
2
3/*
4   This code redirects the user to the exact file to
5   download, and logs the download if it's something
6   we would like to know about (PHP binary or source).
7*/
8
9function get_actual_download_file($file)
10{
11    // Could be a normal download or a manual download file
12    $possible_files = [$file => true, "manual/$file" => false];
13
14    // Find out what is the exact file requested
15    $found = false;
16    foreach ($possible_files as $name => $log) {
17        if (@file_exists($_SERVER['DOCUMENT_ROOT'] . '/distributions/' . $name)) {
18            $found = $name;
19            break;
20        }
21    }
22
23    return $found;
24}
25// Download a file from a mirror site
26function download_file($mirror, $file): void
27{
28    // Redirect to the particular file
29    if (!headers_sent()) {
30        status_header(302);
31        header('Location: ' . $mirror . 'distributions/' . $file);
32    } else {
33        exit("Unable to serve you the requested file for download");
34    }
35
36    // Try to flush output, and make the browser really
37    // download the file, even if the log server is down
38    echo " ";
39    flush();
40}
41