need help with script to play Windows Media Player files

I am trying to create a script that will display Windows Media Player files after making sure the requesting page is authorized to display the file. There are some examples of doing this with graphics files readily available on the web, but I need one for .wmv files. I want to pass the name of the .wmv file in the query string and then get the file from a non public directory, so it cannot be directly linked to.

Here's what I've got so far:


## authorization step goes here

$file = preg_replace('/[^\.\w]/', '', $_GET['file']);

if (!preg_match('/\.\w{3,4}$/', $file, $matches))  exit('file type not recognized');
$matches[0] = strtolower($matches[0]);
if ($matches[0] == '.wmv')
      $mimetype = 'application/x-mplayer2';
      $dir = '/path_to_files/';
else exit('file type not recognized');

if (is_file($dir.$file))
      header('Content-type: '.$mimetype);
} else header( "HTTP/1.0 404 Not Found" );


When I try to run that script, IE seems to be downloading the file, but does not display anything.

I tried renaming the script with a .wmv extension and using a ForceType declaration in the .htaccess file to handle .wmv files as php files. Trying that caused Windows Media Player to launch, but then it says "the source filter for this file could not be loaded." I made sure my browser was correctly playing the file when it was directly available at a web address.

Any suggestions?
Who is Participating?

Improve company productivity with a Business Account.Sign Up

merwetta1Connect With a Mentor Author Commented:
iwarner, as mentioned in my original post, I need to check the referrer before the wmv file is delivered (this is for a leech protection script), so a normal embed will not work. And as I mentioned, directly accessing the wmv files is working fine, so this is not a browser issue.

I never came up with a solution that would work in all browsers, so I'm going to pursue a mod_rewrite solution and PAQ and refund this question. If anyone can help me, please add a comment to this thread and I'll arrange to get the points to you.
first suggestion is try using the mime type


instead of application/x-mplayer2

>> I tried renaming the script with a .wmv

there should not be any need to do that with the header information, keep it as .php

also remove the @ from @readfile($dir.$file);

ie. readfile($dir.$file);

if there is an error reading the file you are currently supressing it
merwetta1Author Commented:
thanks for the suggestions, diablo.

i tried the mime type "video/x-ms-wmv" and removing the "@" you suggested. and i'm using the script as a ".php" file.

when i try it now, it pauses while the video downloads (I'm using a 3 MB test video), then spawns Windows Media Player and gives the error: "the source filter for this file could not be loaded."

during one of my tests, i started output to the browser with a diagnostic message before the header and readfile(), and I saw the 3 MB of text gobbledy gook fill the screen, which i assume is the raw movie file. that leads me to the conclusion that the file is being retrieved and sent to the browser, but just not presented in the right way.
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

merwetta1Author Commented:
let me boil this problem down to it's heart...just the part that will correctly display a .wmv file. Please help me fix this so it works:


$file = '/pathtofile/file.wmv';

if (is_file($file))
      header('Content-type: video/x-ms-wmv');
} else header( "HTTP/1.0 404 Not Found" );

Ok are you are trying to offer up a file that is OUT OF ROOT on your web path.

The code above will work when you are on that server or have the files in your local computer.

Anything delivered via the web must be associated to an IP if you say this is the path, media player play it. it will look for the file in my local path ie c:/pathtofile/efef.wmv

As it isnt there but on your server it will fail.

Most people who do this kind of thing will move a live file to a web address and then delete it after a certain time.

Or you could try streaming the file to the customer this will protect its source anyway.

merwetta1Author Commented:
at the moment, during this testing phase, the .wmv file is in a directory accessible via the web on the same server as the script, but it is called by its full path in the readfile(). It shouldn't matter where the file is using readfile (as long as on same server). I'm pretty sure the file is being delivered to the browser by the script, just not in a way the browser can handle.

i am not trying to protect the source, but rather bandwidth. i don't want other sites to play videos from my webserver on their pages.

You are using an Absolute PATH i.e. $file = '/pathtofile/file.wmv'; though instead of a relative path - i.e.

I would stream the file to the user anyway this will protect source and offer best bandwidth - with REAL server you encode one RM file and the server then throws up this file and decodes to best suit the users bandwidth.


merwetta1Author Commented:
streaming is not an option. still looking for a solution. raising points to 500.
merwetta1Author Commented:
By adding a content-length header, I got the script to start working in IE for Windows. However, it still chokes in Mac browsers (Safari, IE) with the Windows Media Player error "playlist format not recognized". So if anyone knows how to make this script work with Mac browsers, I'd appreciate any suggestions.


$file = 'test.wmv';
header ('Content-type: video/x-ms-wmv');
header ('Content-Length: ' . filesize ($file) . 'bytes');
readfile ($file);

Maybe going at this the wrong way - to play a file you need it to play in the media player on the persons computer realated to that file.

Why not just embed the file in the html page in a popup, i use this for media player - this will show some controls also.

Other than that the only thing I can think of that may make this download is that simply you have not got a program associated with playing WMV files, if it cant find anything then it will tell you to download - so this may be a problem with many users machines. You will have to get them to change the file associations manually.

            STANDBY="Loading Microsoft Windows Media Player components..."
            <PARAM NAME="fileName" VALUE="<?php echo $_GET['source'] ?>">
            <PARAM NAME="animationatStart" VALUE="true">
            <PARAM NAME="transparentatStart" VALUE="true">
            <PARAM NAME="autoStart" VALUE="true">
            <PARAM NAME="showControls" VALUE="true">

            <embed      type="application/x-mplayer2"
                  src="<?php echo $_GET['source'] ?>"
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.