Interruptable file download

Have you ever been to a website that offers downloads of stuff? And while you are waiting for the download, there are lots of ads around? Sure you have. I have too.

The last day I was downloading stuff. An idea came to my head. Was there a site which required to stay and watch the ads while you are downloading? Didn’t see one. Is that technically possible? Sure. I wanted to show how. So I made a small web app for that.

You can see the or the full source.

How does this app work?

  1. The user visits the download page.
  2. He gets through the bushes of ads and clicks on “Download”.
  3. The javascript call makes a request to the server and gets the download URL.
    In the background, PHP script creates a “download is active” file with the
    current modification time.
  4. The download is made through a PHP script. The script checks if the “download
    is active” file has modification time of allowed age. If the file gets older
    than allowed, the download is canceled server-side.
  5. While on this page, after clicking “Download”, there is a invisible counter
    started which calls a “download still active” URL that updates the modification
    time of the “download is active” file. Thus, if the user doesn’t leave the page,
    the download is not canceled.

Do I recommend this or tactics of this like? No. Why?

  1. It is dirty.
  2. Other services exist that offer much better alternatives.
  3. Having the window open doesn’t mean any of the ads will be seen. Better use other tactics.
  4. Only few users will read the “YOU MUST LEAVE THE WINDOW OPEN” notice, files will get corrupted, etc.

All comments and thoughts welcome.

This entry was posted in Code, Ideas and tagged , , , . Bookmark the permalink.

5 Responses to Interruptable file download

  1. Once the download is started, how do you get to interrupt it via PHP? Every X bytes you run a check?

    • stepas says:

      In this example I am checking every 5 seconds. The download is capped @ 4KBs, and there is a sleep() command in the middle of it so it is easy to check. It is easy to check depending on data amount sent or time spent. The main point is to use fread(), but not some full-throttle functions like readfile().

      In production environments it would be harder, because:
      a) each user’s speed is different, so 10 seconds could be 10 kiloytes or 10 megabytes.
      b) on lower quality networks with lots of packet-loss this check can lead to cancelled downloads even it the user is still on the page.
      c) this method could result a DDoS on the download host because of its nature :) This can be avoided by using a WebSocket.
      d) every download is per-PHP-script, this means more RAM and CPU is consumed. Well… Nowdays thats not a big problem. For Google and Twitter :)
      Etc etc.

      PS> your the first to write a comment on my blog. Hooray!

  2. huarong says:

    why not write something about websocket.

    so, I am the second one to leave messages~~~

  3. Mokica says:

    I read a few topics. I respect your work and added blog to favorites.

Leave a Reply

Your email address will not be published. Required fields are marked *


You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="">