can I declare Content-size?

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
eneyas
Forum Newbie
Posts: 1
Joined: Tue Sep 04, 2007 10:39 am

can I declare Content-size?

Post by eneyas »

OK, I'm a total PHP newbie, so maybe this is a dumb question:

I started using a simple PHP file to force browsers to download mp3 files instead of playing them directly in the browser, or trying to instruct the user to right click and choose "save target as..."

it works fine, except i can't figure out a way to declare the size of the MP3. on slow connections, because the browser doesn't know how big the file is, sometimes it cuts it off early and people only get part of the file.

SO, my basic question is this: is there a way to declare the size of the MP3 file?

here is my code currently:



Code: Select all

<?php
header('Content-disposition: attachment; filename=070902_JH_Dwell7.mp3');
header('Content-type: audio/mpeg');
readfile('http://www.lakeshorevineyard.org/07audio/mp3/sm/070902_JH_Dwell7.mp3');
?>


if you want to see what i'm talking about in action, go to: http://www.lakeshorevineyard.org and click on one of the "download" links.



help!
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

And the header is "Content-Length"...

Code: Select all

<?php
$file = "c:/path/to/some/file.extension";


$size = filesize($file);
$content = mime_content_type($file);
$name = basename($file);

header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: public");
header("Content-length: $size");
header("Content-type: $content");
header("Content-Disposition: attachment; filename=$name");
readfile($file);

?>
Post Reply