Jump to content

sending a file to browser


billgates2000

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.
Link to comment
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.
Link to comment
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]
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.