imagine, you have a string variable, which can contain "small", "medium" or "large"
your script has to do certain actions depending on the content of
this variable. You could use
if ($string == "small") ...
else if ($string == "medium") ...
else ....
That's not too bad, you say? Now think of 20 of these different values!
How much easier is sth of this format:
switch ("string")
{
case "small": ...; break;
case "medium": ...; break;
case "large": ...; break;
}
----------------------------------------------------
<a href="download.php?myfile=2">Download avi</a>
<?PHP
$filenr = $_GET['myfile'];
switch ($filenr)
{
case 1: $filename = "files/myvideo.avi"; break;
case 2: $filename = "files/yourvideo.avi"; break;
case 3: $filename = "files/hisvideo.avi"; break;
default: exit;
}
$saveasname = basename($filename);
$filesize = filesize($filename);
header('Content-Type: application/octet-stream');
header('Content-Length: '.filesize($filename));
global $HTTP_USER_AGENT;
if (preg_match('/MSIE 5.5/', $HTTP_USER_AGENT) || preg_match('/MSIE 6.0/', $HTTP_USER_AGENT))
{
header('Content-Disposition: filename="'.$saveasname.'"');
}
else
{
header('Content-Disposition: attachment; filename="'.$saveasname.'"');
}
header('Content-Transfer-Encoding: binary');
readfile($filename);
?>