Jump to content

Archived

This topic is now archived and is closed to further replies.

billgates2000

sending a file to browser

Recommended Posts

Hi,

I have uploaded my mp3 collection on my webserver, but in a folder below the published directory so that it cannot be accessed from the web. However, I would like my friends to be able to download a small part of an mp3 file they request (say, 10%). I found the following script which does the job, i.e. it sends a file from the webserver to the browser:

// Demo - send a (binary) file

$file = "ireland.jpg";
$fp = fopen($file,"r") ;

header("Content-Type: image/jpeg");

while (! feof($fp)) {
$buff = fread($fp,4096);
print $buff;
}



My question is, how can this script be modified so that:

1. It calculates the total size of the file to be send, and
2. Send only 10% of the filesize to the browser.

I have done this in ASP it was really easy, but now I'm moving my site to PHP and I'm really not familliar with this.

Thanks a lot.

Share this post


Link to post
Share on other sites
I guess the simple answer would be to add an ASP tag in if you can, but perhaps it's not to most beautifully elegant method.

Presuming you have streaming you'd probably need to use something like the $_FILES['requested_file']['size'] type command to find out it's size... not really sure how to only download some of it though...
I'll be watching this post with interest.

Share this post


Link to post
Share on other sites
Never done it, but maybe try something like...
[code]
$file = "ireland.jpg";
$fp = fopen($file,"r");
$snippet = ftruncate($fp,filesize($fp) / 10);

header("Content-Type: image/jpeg");

while (!feof($snippet)) {
    $buff = fread($snippet,4096);
    print $buff;
}
[/code]

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.