Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17


need help with script to play Windows Media Player files

Posted on 2004-08-19
Medium Priority
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" );


Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.


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

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

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

This article discusses four methods for overlaying images in a container on a web page
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

688 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