Solved

need help with script to play Windows Media Player files

Posted on 2004-08-19
12
823 Views
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);
      @readfile($dir.$file);
} else header( "HTTP/1.0 404 Not Found" );
exit;

?>

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?
0
Comment
Question by:merwetta1
  • 6
  • 3
12 Comments
 
LVL 27

Expert Comment

by:Diablo84
Comment Utility
first suggestion is try using the mime type

video/x-ms-wmv

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
0
 
LVL 6

Author Comment

by:merwetta1
Comment Utility
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.
0
 
LVL 6

Author Comment

by:merwetta1
Comment Utility
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');
      readfile($file);
} else header( "HTTP/1.0 404 Not Found" );
exit;

?>
0
 

Expert Comment

by:iwarner
Comment Utility
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.

Ian
0
 
LVL 6

Author Comment

by:merwetta1
Comment Utility
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.
0
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 

Expert Comment

by:iwarner
Comment Utility

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

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.

Ian


0
 
LVL 6

Author Comment

by:merwetta1
Comment Utility
streaming is not an option. still looking for a solution. raising points to 500.
0
 
LVL 6

Author Comment

by:merwetta1
Comment Utility
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.

<?php

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

?>
0
 

Expert Comment

by:iwarner
Comment Utility
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.

      <OBJECT
            ID="mediaPlayer"
            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 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"
                  pluginspage="http://www.microsoft.com/Windows/MediaPlayer/"
                  src="<?php echo $_GET['source'] ?>"
                  name="MediaPlayer1"
                  autoStart="true"
                  width="99%"
                  Height="45"
            </embed>
      </OBJECT>
0
 
LVL 6

Accepted Solution

by:
merwetta1 earned 0 total points
Comment Utility
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.
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Suggested Solutions

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

728 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now