Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

need help with script to play Windows Media Player files

Posted on 2004-08-19
12
Medium Priority
?
834 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
ID: 11848221
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
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.
0
 
LVL 6

Author Comment

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

?>
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Expert Comment

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

Ian
0
 
LVL 6

Author Comment

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

Expert Comment

by:iwarner
ID: 11849423

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
ID: 11854589
streaming is not an option. still looking for a solution. raising points to 500.
0
 
LVL 6

Author Comment

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

<?php

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

?>
0
 

Expert Comment

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

      <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
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.
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
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 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…
Suggested Courses

886 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