Anyone know how I could use PHP to program an Html link, so that clicking on it would allow me to download a file from a WebPage.

If not possible in PHP, has anyone seen javascript code which allows the onClick Event to let the file be downloaded to the local machine.

Thanks

    Excuse me but am I missing something here? To make a link in HTML you can either choose to do it the "old" way using a plain HTML-file or have a PHP-"script" (like the one below) make the output :

    <?php
    echo "<a href='myfile.exe'>Download file</a><br>\n";
    ?>

    Can't be all that hard? Or am I missing something?

      The only complication that I can see is this. If the file to be downloaded is in a format that the client's browser has been configured to understand, the browser will open/display the file rather than downloading it.

      For example, if you are trying to download a .txt file, the default action will be for the browser to display the file rather than prompting for a "Save" location.

      One common way around this is to zip the file and link to the zipfile. The browser will then, most likely, open the "Save As" dialog box as required, but the client will be saving the zipped file rather than the original. Anyway, this workaround will be defeated once a zip plugin becomes standard fare for browsers.

      I'm away from my php environment, so I can't experiment, but I would be willing to bet that there is a way of hardcoding the headers so that the client's browser doesn't recognise the .txt file and opens a "Save As" dialog box in desperation.

      Another avenue that might be worth exploring is using javascript to hijack the left-click event on the page, and hardwiring that to the browser's "Save Target As" function.

      Using either of these two methods, however, involves bending the standard web interface. Your visitors may not thank you for that. Ultimately, you may be better of showing them how to right-click and select "Save Target As".

      Just some opinions... they are worth exactly what you think they are. 🙂

      Jack

        That is exactly what my problem is i.e the browser is interpreting the extension(.rt) as one to "link to" rather than one to "Save as". I suppose using Javascript to program the left click button would involve some kind of interaction with the system API. I was wondering what's a good place to see how javascript deals with this...

        Thanks

          5 months later

          hi -

          did you find a workaround for the left click save target as interception problem ?

            I program the right-click on the mouse through the onClick event to pop-up a message, asking the user to left click to download the file.

            cheers

              Write a Reply...