dalehay.com
Search Engine Optimization

PHP Tutorial - vidDetect

Digg! This Page

Introduction

This tutorial will show you how, in PHP, you can detect which player to play certain videos. It might be a bit long winded but you should understand how it all works.

Step 1

When uploading a video you will need to get it to insert the infromation into the database and also have it detect the MIME Type of the video. To do that you can use this varible:
$_FILES['videoname']['type']

Step 2

That'll detect the MIME type of the file. Now we need to create the function. Start by setting the function and giving it 2 properties. ($filename = the filename (eg; mydance.wmv), $folder = the folder where its in (eg; videos)). Then you should connect to the database and get it to pull the the information from it, like the MIME Type (eg; $vd_mime). Then you'll need to create a switch / case option that'll check all the various MIME Types. This one checks WMV, AVI, MPEG and MOV. The code between each of the case statements is the embed codes, you'll notice that they are also calling the properties from the function too - this adds a bit of customisation to it.

Step 3

Once you have done all that just get it to return the filename. And when you have this code in your PHP scripts then all you need to use is

Code:
<?php
vidDetect("video.wmv","videos");
?>

That'll show your video in the correct player.

Code:
<?php
function vidDetect($filename,$folder) {
// vidDetect by Dale Hay
$conn = mysql_connect("DBHOST", "DBUSER", "DBPASS");
mysql_select_db("DBTABLE", $conn) or die(mysql_error());

$sqlvd = "SELECT * FROM videos WHERE filename = '$filename'";
$resultvd = mysql_query($sqlvd, $conn) or die(mysql_error());
while($vd = mysql_fetch_array($resultvd)) {
$vd_mime = $vd['themime'];
}

switch($vd_mime) {
case "video/x-ms-wmv":
?><object width=340 height=300 id="MediaPlayer1" CLASSID="CLSID:22d6f312-b0f6-11d0-94ab-0080c74c7e95" codebase="http://activex.microsoft.com/activex/controls/mplayer/en/nsmp2inf.cab#Version=5,1,52,701"
standby="Loading Microsoft Windows? Media Player components..." type="application/x-oleobject">
<param name="fileName" value="./<?php print $folder; ?>/<?php print $filename; ?>">
<param name="animationatStart" value="true">
<param name="transparentatStart" value="false">
<param name="autoStart" value="false">
<param name="showControls" value="true">
<param name="Volume" value="-450">
<embed type="application/x-mplayer2" pluginspage="http://www.microsoft.com/Windows/MediaPlayer/" src="./<?php print $folder; ?>/<?php print $filename; ?>" name="MediaPlayer1" autostart=0 showcontrols=1 volume=-450 width=340 height=300>
</object><?php
break;
case "audio/x-ms-wmv":
?><object width=340 height=300 id="MediaPlayer1" CLASSID="CLSID:22d6f312-b0f6-11d0-94ab-0080c74c7e95" codebase="http://activex.microsoft.com/activex/controls/mplayer/en/nsmp2inf.cab#Version=5,1,52,701"
standby="Loading Microsoft Windows? Media Player components..." type="application/x-oleobject">
<param name="fileName" value="./<?php print $folder; ?>/<?php print $filename; ?>">
<param name="animationatStart" value="true">
<param name="transparentatStart" value="false">
<param name="autoStart" value="false">
<param name="showControls" value="true">
<param name="Volume" value="-450">
<embed type="application/x-mplayer2" pluginspage="http://www.microsoft.com/Windows/MediaPlayer/" src="./<?php print $folder; ?>/<?php print $filename; ?>" name="MediaPlayer1" autostart=0 showcontrols=1 volume=-450 width=340 height=300>
</object><?php
break;
case "video/x-msvideo":
?><object width=340 height=300 id="MediaPlayer1" CLASSID="CLSID:22d6f312-b0f6-11d0-94ab-0080c74c7e95" codebase="http://activex.microsoft.com/activex/controls/mplayer/en/nsmp2inf.cab#Version=5,1,52,701"
standby="Loading Microsoft Windows? Media Player components..." type="application/x-oleobject">
<param name="fileName" value="./<?php print $folder; ?>/<?php print $filename; ?>">
<param name="animationatStart" value="true">
<param name="transparentatStart" value="false">
<param name="autoStart" value="false">
<param name="showControls" value="true">
<param name="Volume" value="-450">
<embed type="application/x-mplayer2" pluginspage="http://www.microsoft.com/Windows/MediaPlayer/" src="./<?php print $folder; ?>/<?php print $filename; ?>" name="MediaPlayer1" autostart=0 showcontrols=1 volume=-450 width=340 height=300>
</object><?php
break;
case "video/mpeg":
?><object width=340 height=300 id="MediaPlayer1" CLASSID="CLSID:22d6f312-b0f6-11d0-94ab-0080c74c7e95" codebase="http://activex.microsoft.com/activex/controls/mplayer/en/nsmp2inf.cab#Version=5,1,52,701"
standby="Loading Microsoft Windows? Media Player components..." type="application/x-oleobject">
<param name="fileName" value="./<?php print $folder; ?>/<?php print $filename; ?>">
<param name="animationatStart" value="true">
<param name="transparentatStart" value="false">
<param name="autoStart" value="false">
<param name="showControls" value="true">
<param name="Volume" value="-450">
<embed type="application/x-mplayer2" pluginspage="http://www.microsoft.com/Windows/MediaPlayer/" src="./<?php print $folder; ?>/<?php print $filename; ?>" name="MediaPlayer1" autostart=0 showcontrols=1 volume=-450 width=340 height=300>
</object><?php
break;
case "video/quicktime":
?><OBJECT CLASSID="clsid:02BF25D5-8C17-4B23-BC80-D3488ABDDC6B" WIDTH="320" HEIGHT="240" CODEBASE="http://www.apple.com/qtactivex/qtplugin.cab">
<PARAM name="SRC" VALUE="./<?php print $folder; ?>/<?php print $filename; ?>">
<PARAM name="AUTOPLAY" VALUE="true">
<PARAM name="CONTROLLER" VALUE="false">
<EMBED SRC="./<?php print $folder; ?>/<?php print $filename; ?>" WIDTH="320" HEIGHT="240" AUTOPLAY="true" CONTROLLER="false" PLUGINSPAGE="http://www.apple.com/quicktime/download/">
</EMBED>
</OBJECT><?php
break;
case "video/x-quicktime":
?><OBJECT CLASSID="clsid:02BF25D5-8C17-4B23-BC80-D3488ABDDC6B" WIDTH="320" HEIGHT="240" CODEBASE="http://www.apple.com/qtactivex/qtplugin.cab">
<PARAM name="SRC" VALUE="./<?php print $folder; ?>/<?php print $filename; ?>">
<PARAM name="AUTOPLAY" VALUE="true">
<PARAM name="CONTROLLER" VALUE="false">
<EMBED SRC="./<?php print $folder; ?>/<?php print $filename; ?>" WIDTH="320" HEIGHT="240" AUTOPLAY="true" CONTROLLER="false" PLUGINSPAGE="http://www.apple.com/quicktime/download/">
</EMBED>
</OBJECT><?php
break;
default:
echo "ERROR!";
break;
}

return $filename;
}
?>

Comments

Comments:

31st Aug 2008 19:01

Dale a quick note:

Wouldent it be easier to set up a seperate function with varibles within in it that you pass from the switch statement?

It would save a lot of code, and make your code tidier, and easier to understand, i understand that in PHP V4 passing to functions was a tad slow, but things in PHP V5 is a lot quicker.

Failing that set up a class!
By Anthony

31st Aug 2008 18:53

for: Fabion

If you cant read the code, be a better programmer ;)
By Anthony

1st Oct 2007 2:37

hay cool script but dude please try to comment your code please, this is like a shit load of code, but I'm not complaining thanks.
By Fabion

1st Oct 2007 10:47

Fabion - I'll get around to sorting it all out soon. :)
By Dale Hay

Add your comment...

Your Name:

Your Email:

Your Comment:

Enter This Number:

(72321)


Note: Your IP address is saved into the database when you submit a comment. Any type of threatening behaviour will result in your ISP being contacted and legal action being taken place!





Latest News Stories
» Not good...
» Internet Explorer 8 Problems
» October
» Relaxtion by Dale Hay
» BBC Weather Revamp
» Dog Singing
Top Viewed Stories
» PHP Mini Message Board Tutorial (4,211)
» Rare McDonalds Monopoly 2008 Tickets (3,063)
» PHP Search Engine Tutorial (2,272)
» Helen Willetts Pregnant? (1,400)
» QI Series 6 on BBC One (900)
» Create your own Image Uploader (873)
Most Commented Stories
» PHP Mini Message Board Tutorial (33)
» Create your own Image Uploader (18)
» I like my DuMP (15)
» I'm on Wikipedia! (7)
» Chocie Munchin Niece (6)
» Life on Mars... (6)