Domain Empire

Php - Get file extension from a URL string and display div based on file type

Spaceship Spaceship
Watch
I'm passing the string:

?filename=MovieName.wmv or MovieName.mp4

and I need the script to display a div with the corresponding media player based on the file extension.

Here's the basic structure:

<?php
// Movie Player
// Read movie name from URL
$$filename = $_GET['$filename'];

if ($filename != '') :
?>
<html>
<body>

*/ if filetype = .wmv display this div
<div id="windows_media">

*/ if filetype = .mp4 display this div
<div id="quicktime">

</body>
</html>
<?php endif; ?>

From here, I'm stumped. Any help is greatly appreciated!
 
0
•••
The views expressed on this page by users and staff are their own, not those of NamePros.
$$filename should only have one $.

Basically you want to use the explode() command on $filename
$ext = explode(".", $filename);
echo $ext[1]; // will give you the extension.

The above will take the filename and create an array with the filename and extension in it.
http://php.net/manual/en/function.explode.php

Then in your php just do
if($ext[1]=="wmv") echo wm div
else echo quicktime



An optional method you can use to prevent the errors if there are "." in the filename is to use the string length and get the last 3 characters of the filename. Here is an example of this method: http://www.wallpaperama.com/forums/how-to-get-the-last-2-characters-in-a-string-php-t6438.html
 
1
•••
PHP:
<?php

// Movie Player
// Read movie name from URL
$filename = trim($_GET['filename']);
$filetype = substr(strstr($filename, '.'), 1);

if ($filename != ''):
?>
<html>
<body>

<?php

switch ($filetype)
{
	case 'wmv':
		echo '<div id="windows_media">..code..</div>';
		break;
	case 'mp4':
		echo '<div id="quicktime">..code..</div>';
		break;
	case '':
	default:
		echo '<p>Invalid filetype.</p>';
		break;
}

?>

</body>
</html>
<?php endif; ?>
 
1
•••
1
•••
  • The sidebar remains visible by scrolling at a speed relative to the page’s height.
Back