Can anyone tell me the best method to download a file using PHP?

I have links on a page which enable files to be downloaded, but I want to record the number of times the file has been downloaded.

Is it possible also to detect if the file has been downloaded rather than simply just record the number of times the link to the file has been clicked. They could of course cancel the download.

Any ideas please?

Thanks in advance.

BB

    The following should work:

    Most click scripts will redirect to file. what u do instead is:-

    instead of giving header("Location: [url]http://.../.../.../[/url] )"

    pass the other header (just forget this, its has some 'attachement' text in it, plz search the forums ofr this, i am a bit busy right now, u will find this header here only, very common)

    after open the file and start echoing its contents. after u have echoed all contents update the db

    Note: if user cancels just when the download is almost complete

      <?

      // suppose $file is the filename and $filepath has path to the file
      // to be downloaded
      // You will have to so some work to get these variables to set.

      $file = "player.zip";
      $filepath= "/path/to/download/dir";

      header("Content-type: application/octet-stream\n");
      header("Content-disposition: attachment;filename=$file\n");
      readfile($filepath."/".$file);

      // now just increment the counter.
      // this should work

      ?>

        2 months later

        if i use this method to include the data and change the header, what about large files?
        will this tie up the server and fill up its memory?
        or will it be about the same server load as downloading a file directly from the link?
        also using this will someone downloading this file have any access at all to the original file?

        i will be selling files, and i want to use php to authorize the download, so i dont want people to have any access to where teh original file is stored. i am also considering blocking all access to that directory except by the localhost so they can only get it through that link anyway.

          Well in case of large files, using php method will take more load.
          Also this method will not support resuming, especially needed in case of larger files.

          you may keep the files outside your webroot. so no one can link directly to the file.

            so in the case that i do keep them outside of the webroot folder, how can i link to them? these files will be mostly between 3 and 5 megs.

              link them using the php file containing the code above.

                Write a Reply...