Headers for download

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Emerald
Forum Newbie
Posts: 7
Joined: Mon Aug 25, 2003 10:59 am

Headers for download

Post by Emerald »

:cry:
I have a download script that inludes this page to get the url of the file to download. The file is .wma (Windows Media) format.
My problem with this is that when it brings up the popup download box, it says: You have chosen to download a file.
10.wma from http://www.mywebsite.com.
When I choose save, it downloads a "blank" file in like 2 sec. (It's a 4Mb file).
What seems to be the problem???

CODE:
<?

header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");

header("Content-Type: application/octet-stream");

header("Content-Disposition: attachment; filename=".basename($url).";");
header("Content-Transfer-Encoding: binary");
header("Content-Length: ".filesize($url));

?>

($url is the whole web url of the file)
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

after the header("Content-Length: ".filesize($url));you send the content via readfile?
Emerald
Forum Newbie
Posts: 7
Joined: Mon Aug 25, 2003 10:59 am

Post by Emerald »

I have used :
readfile("$url");
in the code also.
What it does is show the progess bar loading in the browser for a long time before the download box will appear. Then when it does, & i save it does the same thing. Downloads a blank file in 2 sec.
It didn't make a difference in the script.

Does it make a difference if I'm referring to a URL versus a server location like d:/website/www/folder/file.wma?
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

do you mean $url looks like 'http://some-serv.er/a/file.wma' ?
Emerald
Forum Newbie
Posts: 7
Joined: Mon Aug 25, 2003 10:59 am

Post by Emerald »

Yes.
It picks up file.wma when the download box appears(after a long wait) & then when saving it, it basically saves nothing.
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

then at least filesize() will fail
http://de.php.net/manual/en/function.filesize.php wrote:Note: This function will not work on remote files as the file to be examined must be accessible via the servers filesystem.
Emerald
Forum Newbie
Posts: 7
Joined: Mon Aug 25, 2003 10:59 am

Post by Emerald »

I have made a url entry in the database as a local file on server.
My local path is /usr/sites/blahblah.
I noticed the download box popup quick, but still ain't downloading nothing.
How can I tell it to get the file on the machine.
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

you script now looks like

Code: Select all

<?php
// $url is a local path like /usr/sites/dlfile1
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Content-Type: application/octet-stream");
header("Content-Disposition: attachment; filename=".basename($url).";");
header("Content-Transfer-Encoding: binary");
header("Content-Length: ".filesize($url));
readfile($url);
?>
?
You might check those values

Code: Select all

<pre><?php
echo("Pragma: public"), "\n";
echo("Expires: 0"), "\n";
echo("Cache-Control: must-revalidate, post-check=0, pre-check=0"), "\n";
echo("Content-Type: application/octet-stream"), "\n";
echo("Content-Disposition: attachment; filename=".basename($url).";"), "\n";
echo("Content-Transfer-Encoding: binary"), "\n";
echo("Content-Length: ".filesize($url)), "\n";
?></pre>
perhaps they're not what you suppose them to be.
Emerald
Forum Newbie
Posts: 7
Joined: Mon Aug 25, 2003 10:59 am

Post by Emerald »

It reads filename="aSong.wma", but it don't display anything for the Content-Length.
Thanks for trying to help me out.
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

sounds to me like the path in $url is wrong.

Code: Select all

<pre><?php
if (!is_file($url))
	echo "'$url' is not a valid file/path";
echo("Pragma: public"), "\n";
echo("Expires: 0"), "\n";
echo("Cache-Control: must-revalidate, post-check=0, pre-check=0"), "\n";
echo("Content-Type: application/octet-stream"), "\n";
echo("Content-Disposition: attachment; filename=".basename($url).";"), "\n";
echo("Content-Transfer-Encoding: binary"), "\n";
echo("Content-Length: ".filesize($url)), "\n";
?></pre>
Emerald
Forum Newbie
Posts: 7
Joined: Mon Aug 25, 2003 10:59 am

Post by Emerald »

The page originates from dl.php?id=3
which brings up the code/message:

'http://goldenstreetmusic.sitestream.com/Beethoven.wma' is not a valid file/path
Pragma: public
Expires: 0
Cache-Control: must-revalidate, post-check=0, pre-check=0
Content-Type: application/octet-stream
Content-Disposition: attachment; filename=Beethoven.wma;
Content-Transfer-Encoding: binary
Content-Length:

but the file location does exist.
The script will not recognize root paths such as /usr/sites/mywebsite.com/www/folder/
Now, what should I do?

Shouldn't it be able to find http:// locations?
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

there are functions that can only handle local files (accessed through the localfile system) and those that operate with url_wrappers.
filesize() can only access local files
http://php.net/manual/en/function.filesize.php wrote:Note: This function will not work on remote files as the file to be examined must be accessible via the servers filesystem.
The script will not recognize root paths such as /usr/sites/mywebsite.com/www/folder/
why is that?
Emerald
Forum Newbie
Posts: 7
Joined: Mon Aug 25, 2003 10:59 am

Post by Emerald »

Here is an update to my problem. My host have gave me this code to generate the download box.

Code: Select all

header( "Content-Type: application/force-download" );
header( "Content-Disposition: attachment; filename=Test.wma"); 
$fn=fopen( "download/folder/theFile.wma" , "r" );
fpassthru( $fn );
this works good when you physically type the file name...it will download & I can play the file, but fopen is having a problem with using $url.
I don't know if its because my files are .wma format because when $url is used & I want to play the song, Windows Media gives an error: "The file you are attempting to play has an extension that does not match the file format." And it will not play.

I have told $url (generated from a query using an [id]) to print; & it does give.. download/folder/theFile.wma
the same as physically typing it. Does fopen not like variables?

Code: Select all

$fn=fopen( "$url" , "r" );
I haven't seen examples with people doing it.
Post Reply