Download headers test

This doesn’t make a difference to how the data is actually sent – it only changes the HTTP header which says how it’s encoded.

Source code

<?php

//check if we have the necessary data from the user to output a file
if(
    isset(
$_GET["download"])
    && isset(
$_GET["content"])
    && isset(
$_GET["mimetype"])
    && isset(
$_GET["encoding"])
    && isset(
$_GET["extension"])
) {
    
//set the various vars
    
switch($_GET["content"]) {
        case 
"pdf":                $file "pdf.pdf"$filebase "pdf"; break;
        case 
"binary":            $file "binary.bin"$filebase "binary"; break;
        case 
"text": default:    $file "text.txt"$filebase "text"; break;
    }
    switch(
$_GET["mimetype"]) {
        case 
"pdf":                $type "application/pdf"; break;
        case 
"binary":            $type "application/octet-stream"; break;
        case 
"text": default:    $type "text/plain"; break;
    }
    switch(
$_GET["encoding"]) {
        case 
"7bit":            $encoding "7bit"; break;
        case 
"binary": default:    $encoding "binary"; break;
    }
    
$prompt = isset($_GET["prompt"]) && $_GET["prompt"] == "true";
    switch(
$_GET["extension"]) {
        case 
"none":            $ext ""; break;
        case 
"pdf":                $ext ".pdf"; break;
        case 
"bin":                $ext ".bin"; break;
        case 
"txt": default:    $ext ".txt"; break;
    }
    
$nocache = isset($_GET["nocache"]) && $_GET["nocache"] == "true";
    
    
//output the headers, spit out the file and exit
    
if($nocache) {
        
header("Pragma: no-cache");
        
header("Cache-Control: no-cache, must-revalidate");
        
header("Expires: " date("r"strtotime("-1 year")));
    }
    
header("Content-Type: $type");
    
header("Content-Disposition: " . ($prompt "attachment; " "") . "filename=\"$filebase$ext\"");
    
header("Content-Transfer-Encoding: $encoding");
    
header("Content-Length: " filesize("files/$file"));
    
readfile("files/$file");
    exit();
}

//output an HTML form
include "/srv/www/urchincode.php";
include 
"/srv/www/00scripts/functions.php";

header("Content-Language: " . (defined("SITE_LANGUAGE") ? SITE_LANGUAGE "en"));
header("Content-Style-Type: text/css");
header("Content-Script-Type: text/javascript");
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <title>Download headers test</title>
    <meta name="description" content="Tests what happens when the browser is given a non-HTML file with various header combinations">
    <meta name="author" content="Bart Nagel of bnagel.net">
    <link href="http://perihelion.tremby.net/stylesheet/styles.css.php?hue=60" rel="stylesheet" type="text/css">
    <style type="text/css">
        small {
            width: 30em;
        }
    </style>
    <link href="/favicon.png" rel="shortcut icon" type="image/png">
</head>
<body>

<h1>Download headers test</h1>

<form action="<?php echo $_SERVER["PHP_SELF"]; ?>" method="GET">
    <table class="gridform">
        <tr>
            <th><label>Content</label></th>
            <td class="set">
                <label for="content_0">
                    <input type="radio" class="inputcheckboxradio" id="content_0" name="content" value="pdf" checked="checked">
                    PDF
                </label>
                <label for="content_1">
                    <input type="radio" class="inputcheckboxradio" id="content_1" name="content" value="binary">
                    Binary data
                </label>
                <label for="content_2">
                    <input type="radio" class="inputcheckboxradio" id="content_2" name="content" value="text">
                    Plain text
                </label>
            </td>
        </tr>
        <tr>
            <th><label>Mime type</label></th>
            <td class="set">
                <label for="mimetype_0">
                    <input type="radio" class="inputcheckboxradio" id="mimetype_0" name="mimetype" value="pdf" checked="checked">
                    application/pdf
                </label>
                <label for="mimetype_1">
                    <input type="radio" class="inputcheckboxradio" id="mimetype_1" name="mimetype" value="binary">
                    application/octet-stream
                </label>
                <label for="mimetype_2">
                    <input type="radio" class="inputcheckboxradio" id="mimetype_2" name="mimetype" value="text">
                    text/plain
                </label>
            </td>
        </tr>
        <tr>
            <th><label>Encoding</label></th>
            <td class="set">
                <label for="encoding_0">
                    <input type="radio" class="inputcheckboxradio" id="encoding_0" name="encoding" value="7bit">
                    ASCII (7-bit)
                </label>
                <label for="encoding_1">
                    <input type="radio" class="inputcheckboxradio" id="encoding_1" name="encoding" value="binary" checked="checked">
                    Binary (8-bit)
                </label>
                <small>This doesn&rsquo;t make a difference to how the data is actually sent &ndash; it only changes the HTTP header which <i>says</i> how it&rsquo;s encoded.</small>
            </td>
        </tr>
        <tr>
            <th><label for="prompt">Save prompt</label></th>
            <td><input type="checkbox" class="inputcheckboxradio" id="prompt" name="prompt" value="true" checked="checked"></td>
        </tr>
        <tr>
            <th><label>File extension</label></th>
            <td class="set">
                <label for="extension_0">
                    <input type="radio" class="inputcheckboxradio" id="extension_0" name="extension" value="pdf" checked="checked">
                    .pdf
                </label>
                <label for="extension_1">
                    <input type="radio" class="inputcheckboxradio" id="extension_1" name="extension" value="bin">
                    .bin
                </label>
                <label for="extension_2">
                    <input type="radio" class="inputcheckboxradio" id="extension_2" name="extension" value="txt">
                    .txt
                </label>
                <label for="extension_3">
                    <input type="radio" class="inputcheckboxradio" id="extension_3" name="extension" value="none">
                    <i>[none]</i>
                </label>
            </td>
        </tr>
        <tr>
            <th><label for="nocache">&ldquo;No caching&rdquo; headers</label></th>
            <td><input type="checkbox" class="inputcheckboxradio" id="nocache" name="nocache" value="true" checked="checked"></td>
        </tr>
        <tr>
            <th></th>
            <td><input type="submit" class="inputbutton" id="download" name="download" value="Get file"></td>
        </tr>
    </table>
</form>

<h2>Source code</h2>
<div class="boxout" style="height: 20em;">
<?php echo preg_replace('/(<[bh]r) ?\/>/''${1}>'highlight_file(__FILE__true)); ?>
</div>

<?php echo tracker(URCHIN_CODE_PERIGEE); ?>

</body>
</html>