need help with script to play Windows Media Player files

Posted on 2004-08-19
Last Modified: 2008-01-09
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?
Question by:merwetta1
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 3
LVL 27

Expert Comment

ID: 11848221
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

Author Comment

ID: 11848371
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.

Author Comment

ID: 11848442
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" );

[Live Webinar] The Cloud Skills Gap

As Cloud technologies come of age, business leaders grapple with the impact it has on their team's skills and the gap associated with the use of a cloud platform.

Join experts from 451 Research and Concerto Cloud Services on July 27th where we will examine fact and fiction.


Expert Comment

ID: 11849347
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.


Author Comment

ID: 11849393
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.

Expert Comment

ID: 11849423

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.



Author Comment

ID: 11854589
streaming is not an option. still looking for a solution. raising points to 500.

Author Comment

ID: 11908264
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);


Expert Comment

ID: 11908643
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'] ?>"

Accepted Solution

merwetta1 earned 0 total points
ID: 11977847
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.

Featured Post

Why Off-Site Backups Are The Only Way To Go

You are probably backing up your data—but how and where? Ransomware is on the rise and there are variants that specifically target backups. Read on to discover why off-site is the way to go.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
There are times when I have encountered the need to decompress a response from a PHP request. This is how it's done, but you must have control of the request and you can set the Accept-Encoding header.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

617 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question