/    Sign up×
Community /Pin to ProfileBookmark

Code to facilitate a file download

I have some programming experience in other languages but have not programmed in PHP. Therefore I am not even certain if PHP is the correct or most suitable tool to use for this purpose and consequently if I am posting this to the correct Forum.

I want to enable a visitor to a site to click on a “Download” button which will then allow them to download and save a file locally to disk. I assume that some server-side programming will be necessary to enable this process to complete. However, I am uncertain as to what is the best approach to adopt to facilitate this.

Currently security is not an issue, however, at some time in the future certain files that are made available for download will have to be housed on a secure server that will require a Username / Password to access. Even though security will not be enabled at this time, I would prefer to set the process up in such a way that security could be added reasonably easily at a future date.

I would be grateful to receive any advice as to the preferred method to employ to facilitate this process.

Thanks

T.

to post a comment
PHP

5 Comments(s)

Copy linkTweet thisAlerts:
@ellisglAug 01.2007 — You can just link to the file most of the time and the browser will ask what to do with it.
Copy linkTweet thisAlerts:
@TabvlaauthorAug 02.2007 — Hi ellisgl, thanks for your reply.

  • 1. Could you provide some specific example in HTML or JavaScript of how this would be coded on the Client-side.


  • 2. On the Server-side what would be the best procedure to use to facilitate file security, whereby a user would have to enter their username/password to access the download. (I could use a protected directory but that would mean the same username/password for all users and that is not acceptable).


  • 3. Is there a method which could record download details, for example :

  • - number of downloads

  • - time of download

  • - IP address of user

  • - username (when security has been implemented)


  • Thanks in advance for any suggestions which you may have.

    T.
    Copy linkTweet thisAlerts:
    @foxbeeflyAug 02.2007 — Yep: PHP/MySQL is what I would use to achieve all this:

    You would need a database:

    1) with a table to store details of registered users: user_id, name, password etc.

    2) with a table to store details of the files available for download: file_id, file_name, file_URL

    3) a table to record details of downloads: user_id, file_id, date_downloaded

    You would need PHP pages:

    1) to allow users to Register, and login

    2) a page that echo's all the downloads available in the download table as links

    3) when a registered user click on the link to download the file, the link not only downloads the file, but updates the download table with the details of THAT download.

    If you're up for the challenge, I would be more than happy to assist further!
    Copy linkTweet thisAlerts:
    @hastxAug 03.2007 — for a simple solution all you need is the download script, this would be a page called send_to_client.php

    [code=php]
    <?php

    $filename = $_GET['path'];
    $save_as = $_GET['title'];
    $file_size = filesize("$filename");
    if ($fp = fopen("$filename", 'rb')) {

    Header('Content-Type: application/pdf');
    Header('Content-Disposition: attachment; filename="'.$save_as.'"');
    Header('Content-Length: '.$file_size);
    fpassthru($fp);
    } else {
    echo"cannot find file";
    }

    ?>
    [/code]


    Then your download links would look like
    [CODE]
    <a href="send_to_client.php?path=downloads/file.pdf&title=file.pdf">Download</a>
    [/CODE]


    As was mentioned before, this may be an unnecessary exercise, because if your downloads are of a common type(like .zip or .pdf) the browser is already going to know what to do with it.

    as far as security, there are multiple levels of security, so depending on how secure you want to get, you could just include any number of free login scripts on the actual download page. There are even scripts that can make a user register and then email them a link to the download to ensure you get a real email.

    edit:

    I remebered this thread, whick is where the code above came from

    http://www.webdeveloper.com/forum/showthread.php?t=147248&highlight=inline+file
    Copy linkTweet thisAlerts:
    @TabvlaauthorAug 06.2007 — Thanks Foxbeefly and Hastx for your helpful feedback.

    Yes, I am most certainly up for this.

    As I mentioned in my first post I do have some programming experience (VB, JavaScript) but not in PHP, therefore I am not even certain if this is the correct tool to use to achieve the objective - so in this regard I will trust your judgment as to the way forward.

    A key requirement is that the process should be automatic with no need for human intervention.

    The top-level steps in designing the process will be as per attached flow diagram.

    This is the process at a high-level. Obviously there are many more sub-steps in the process, specifically with regards to error-trapping. But the flow diagram contains all the key steps.

    My greatest concern at the moment is that I use the correct tools to create this process. I don't want to invest a considerable amount of time and effort only to find that the end result could have been achieved faster, more effectively and with far less effort had I used a different set of tools.

    The websites will be located with an ISP that provides Microsoft Hosting. This includes ASP.NET; MS Access and MS SQL.

    Thanks in advance for your feedback.

    T.

    [upl-file uuid=eb88d315-b87e-4742-aea0-0bff1d45b634 size=39kB]File Download Control.jpg[/upl-file]
    ×

    Success!

    Help @Tabvla spread the word by sharing this article on Twitter...

    Tweet This
    Sign in
    Forgot password?
    Sign in with TwitchSign in with GithubCreate Account
    about: ({
    version: 0.1.9 BETA 5.3,
    whats_new: community page,
    up_next: more Davinci•003 tasks,
    coming_soon: events calendar,
    social: @webDeveloperHQ
    });

    legal: ({
    terms: of use,
    privacy: policy
    });
    changelog: (
    version: 0.1.9,
    notes: added community page

    version: 0.1.8,
    notes: added Davinci•003

    version: 0.1.7,
    notes: upvote answers to bounties

    version: 0.1.6,
    notes: article editor refresh
    )...
    recent_tips: (
    tipper: @Yussuf4331,
    tipped: article
    amount: 1000 SATS,

    tipper: @darkwebsites540,
    tipped: article
    amount: 10 SATS,

    tipper: @Samric24,
    tipped: article
    amount: 1000 SATS,
    )...