Jump to content


Photo

sending a file to browser


  • Please log in to reply
2 replies to this topic

#1 billgates2000

billgates2000
  • New Members
  • Pip
  • Newbie
  • 1 posts

Posted 25 May 2006 - 08:47 AM

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.

#2 gerkintrigg

gerkintrigg
  • Members
  • PipPipPip
  • Magician
  • 828 posts
  • LocationBristol, UK
  • Age:37

Posted 25 May 2006 - 02:02 PM

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.
Neil Trigger - http://www.ghostlypublishing.co.uk - Ghostly Publishing - Children's Fantasy Books

#3 trq

trq
  • Staff Alumni
  • Advanced Member
  • 31,041 posts

Posted 25 May 2006 - 02:15 PM

Never done it, but maybe try something like...
$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;
}





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users