note 75091 deleted from function.header by felipe
| From: | felipe@php.net | Date: | Sun, 30 Dec 2007 18:32:16 +0000 |
| Subject: | note 75091 deleted from function.header by felipe | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-134384@lists.php.net to get a copy of this message | ||
Note Submitter: Michael Keyser
----
I use this to stream and download media files:
<?php
if (isset($_GET['action']) && isset($_GET['file']) &&
file_exists(stripslashes($_GET['file']))) {
$file = stripslashes($_GET['file']);
$base = basename($file);
$extension = "";
$i = strlen($base);
while (substr($base, $i, 1) != ".") {
$extension = substr($base, $i--, 1) . $extension;
}
switch (strtolower($extension)) {
case "wmv":
case "mpg":
case "mpeg":
case "avi":
case "mov":
include "../mysql_settings.php";
$con = mysql_connect($mysql_host, $mysql_user, $mysql_password) or die(mysql_error());
mysql_select_db($mysql_database) or die(mysql_error());
switch ($_GET['action']) {
case "download":
$query = "INSERT INTO downloads (file,ip_address) VALUES ('".
addslashes($file) ."','". $_SERVER['REMOTE_ADDR']
."')";
mysql_query($query) or die(mysql_error());
mysql_close($con);
$message = "Hello Michael,<br><br>\n"<b>". $file
."</b>" has been downloaded from your website at ". date("g:i:s A - M
j, Y") .".<br><br>\nIP Address: <b>".
$_SERVER['REMOTE_ADDR'] ."</b>";
email($file ." has been downloaded from your website!", $message);
// tell the browser how big the file is so the user has a percentage complete
progress bar
header("Content-Type: ". $extension);
header("Content-Length: ". filesize($file));
header("Content-Disposition: attachment; filename=\"".
str_replace(" ", "%20", $base) ."\"");
readfile($file);
break;
case "stream":
$query = "INSERT INTO stream (file,ip_address) VALUES ('".
addslashes($file) ."','". $_SERVER['REMOTE_ADDR']
."')";
mysql_query($query) or die(mysql_error());
mysql_close($con);
$message = "Hello Michael,<br><br>\n"<b>". $file
."</b>" is or has been streamed from your website at ". date("g:i:s A
- M j, Y") .".<br><br>\nIP Address: <b>".
$_SERVER['REMOTE_ADDR'] ."</b>";
email($file ." is or has been streamed from your website!", $message);
header("location: ". str_replace(" ", "%20",
$base));
break;
default:
// action is invalid
break;
}
break;
default:
// invalid file extension
break;
}
}
?>