1

I have hosted some files on a free website hosting account.

I am used to transferring my files via an FTP client and it works well. But sometimes I need to download the files via a web browser. Here is where I face some problems. The most important thing for me is the time for starting download or the time for the file to be downloaded.

Is this caused by the specification of the server? Or is there some configuration that I have to set? Or may be it is caused by the limit of download speed - is 'download speed' affected by the website's bandwidth?

I'm really new to web hosting management, so forgive me if the topic is too basic to ask.

1 Answer 1

3

Time till start download

The startingtime depends (mainly) on:
- Time it takes to run the PHP code (this should take little time)
- The time it takes before the first data is send

The latter one is the main contributor, you want to send (some) data as fast as you can. If you echo a file_get_contents() of a file, you would have to wait untill all the data is send to the browser (essentially the whole download it will blank, and at the end the whole file will be complete in 1 go). What you want is to send it in chucks, send small pieces real fast.

Examplecode which sends 1024 bytes per itteration:

// OPENING THE FILE
$file = fopen($filepath, "r");
$bufferlength = 1024;
while (!feof($file)){
    // READ A BUFFER SIZE OF THE FILE
    $buffer = fread($file, $bufferlength);
    echo $buffer;
    $buffer = "";
    flush(); // THIS IS ESSENTIAL FOR LARGE DOWNLOADS
}
fclose($fp);

The max speed here is: $bufferlength*(itterations in 1 sec) bytes/sec.
You could use algorithms to make it send small chunks in the beginning, increasing in size the further it progresses.

Time to complete download

This is influenced by the code above, but that will be fast enough for 99% of the cases.
The main cause here is bandwidth. Cheap hosts are not premium hosts, you don't get much bandwidth. The speed at which you can upload is limited, and if multiple users want to download something, this will get shared amongst them.

3
  • 1
    +1 @Martijn. In addition you might want to consider using Amazon S3 (inexpensive) to host files for which the download speed is more important, or CloudFlare (free) to serve a cached copy of your website/files for some increased performance. Commented Oct 22, 2014 at 11:22
  • So, the download speed could be customized, right? And my bad, I don't use any PHP file in the directory, I (user) am directly grab the file. Thanks for the lesson Commented Oct 23, 2014 at 4:30
  • It can be influenced, the actual top speed is not within your control, thats the connections in the datacenter. And if you dont use php, you can't have the benefit of the first half af my answer
    – Martijn
    Commented Oct 23, 2014 at 7:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.