0

I have a server with hundreds of audio files on it (I own all of them by the way) where people can download them for free. I use the following PHP script to download the files:

<?php
header('Content-disposition: attachment; filename=audio.mp3');
header('Content-type: audio/mpeg');
readfile('Part1.mp3');

My viewers have complained, however, that it can take up to 30 seconds just for the download to begin. What can I do to improve the initialization speed of the download? Is it possible?

Additional Info:

My site is hosted by iPage.

Liftoff
  • 24,717
  • 13
  • 66
  • 119
  • 2
    I'm not sure about the legality to distribute "licensed" copy. But anyway... Hope you got your answer. – Andreas Wong Mar 15 '12 at 03:09
  • By that I mean I own all of the content I am offering. – Liftoff Mar 15 '12 at 03:09
  • Are you using Apache or other HTTP servers? What is the size of the files? Did you check the access log? – Reci Mar 15 '12 at 03:10
  • I'm sorry I'm kind of new to this kind of stuff. I know that the platform is Debian, the file sizes all range from 40mb to 80mb, and the access log is a long list of gibberish to me. – Liftoff Mar 15 '12 at 03:13
  • faster server, more ram, larger bandwidth, this kind of thing can consume some serious resources. –  Mar 15 '12 at 03:17
  • I think `readfile()` reads the whole file into memory and return. 40mb-80mb might mean something in a shared host. Try some small files and see if still slow. – Reci Mar 15 '12 at 03:17
  • An 18.4mb file took about 5 seconds, so yes I think you are right; I don't know another way to download the files though, so if someone could explain to me how to echo file segments or how to use mod_xsendfile (if iPage even offers that), that would be great. – Liftoff Mar 15 '12 at 03:20

1 Answers1

5

Instead of using readfile() to read the entire file into memory and send it, read it in chunks, echo each chunk out as it's read, and flush after each. Better yet, see if mod_xsendfile or its equivalent is available, and use it to send the file.

Ignacio Vazquez-Abrams
  • 776,304
  • 153
  • 1,341
  • 1,358