PHP Script Error

dgreenjr26
dgreenjr26 used Ask the Experts™
on
Im trying to write a PHP script that will allow the downloading of Windows Media Video files (.wmv) I came up with this:

header('Content-Description: File Transfer');
header("Content-type: application/force-download");  
header("Content-type: video/x-ms-wmv");
header('Content-Disposition: attachment; filename='.basename($file));
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
header('Content-Length: ' . filesize($file));
ob_clean();
flush();
readfile($Dfile);

The problem is that it will bring up a save as dialog box for anything else but .wmv files. I was ale to download a .mp3 and .jpg etc... but for some reason .wmv will not come up it just dies. Can anyone help me with this, maybe tell me what Im missing?
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Dave BaldwinFixer of Problems
Most Valuable Expert 2014

Commented:
What happens if you link to the same WMV file without the headers?
dgreenjr26IT Director

Author

Commented:
Do you mean just using:

readfile($Dfile);

without the preceding headers?
Dave BaldwinFixer of Problems
Most Valuable Expert 2014

Commented:
No, I mean <a href="yoursite.com/your.wmv">Your WMV>/a> .  A plain link so that the PHP isn't in the way.
How to Generate Services Revenue the Easiest Way

This Tuesday! Learn key insights about modern cyber protection services & gain practical strategies to skyrocket business:

- What it takes to build a cloud service portfolio
- How to determine which services will help your unique business grow
- Various use-cases and examples

dgreenjr26IT Director

Author

Commented:
Its Streams to Windows Media Player doesn't bring up a save as dialog box.
Dave BaldwinFixer of Problems
Most Valuable Expert 2014

Commented:
Ok, that shows that is accessible on the web site.  Which browser are you using and can you try it in Firefox or Chrome?  I ask because opening in Windows Media Player would be the default in Internet Explorer but not necessarily in the others.
dgreenjr26IT Director

Author

Commented:
I have tried the script on IE, Firefox & Safari and all of them just die. But if I were to try one of the other file like mp3 or jpg it brings up the save as dialog box for all 3 browsers.
Dave BaldwinFixer of Problems
Most Valuable Expert 2014

Commented:
Try one of the simpler versions on this page: http://php.net/manual/en/function.header.php  Also if your WMV file name has a space in it, you might want to modify the line with the filename so that the filname is enclosed in double quotes like below.
header('Content-Disposition: attachment; filename="'.basename($file).'"');

Open in new window

dgreenjr26IT Director

Author

Commented:
Im new to PHP so do you mean use something like this:

header("Location: http://www.example.com/test.wmv");
Dave BaldwinFixer of Problems
Most Valuable Expert 2014

Commented:
No.  If you look at the "Content-Disposition:" header you have, you will see that the filename could have spaces in it and be misinterpreted if it is not quoted.  I added double quotes to your line above.  If your original filename does not have spaces in it, it doesn't matter.
dgreenjr26IT Director

Author

Commented:
No the original file name does not have spaces in it. I have tried multiple wmv files and none of them have spaces in the file name.
Fixer of Problems
Most Valuable Expert 2014
Commented:
Here are a pair of files for you that work for me.  Save the HTML file as 'test.html' and save the PHP file as 'phpdnld.php' in the same directory.  Change the image files to the names of files that you actually have and try it.
<?php 
$file = $_GET['fn'];
$fext = explode(".",$file);
header('Content-Description: File Transfer');
if($fext[1] == 'wmv') {
	//header("Content-type: application/force-download");  
	header("Content-type: video/x-ms-wmv");
	}
if($fext[1] == 'jpg') {
	header("Content-type: image/jpeg");
	}
if($fext[1] == 'pdf') {
	header('Content-type: application/pdf');
	}
header('Content-Disposition: attachment; filename="'.basename($file).'"');
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
header('Content-Length: ' . filesize($file));
//ob_clean();
//flush();
readfile($file);
?>

Open in new window

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
 "http://www.w3.org/TR/html4/loose.dtd">

<html>
<head>
<title>PHP download test</title>
</head>
<body>
<h1>PHP download test</h1><br>
<a href="phpdnld.php?fn=bud4.wmv">bud4.wmv</a><br><br>
<a href="phpdnld.php?fn=BrightEyes.jpg">BrightEyes.jpg</a><br><br>
<a href="phpdnld.php?fn=980601-001A.pdf">980601-001A.pdf</a><br><br>

</body>
</html>

Open in new window

dgreenjr26IT Director

Author

Commented:
That worked thanks!!!
Dave BaldwinFixer of Problems
Most Valuable Expert 2014

Commented:
Good, glad to help.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial