** HELP WITH DOWNLOADS **

10 replies
Hi,

I have a little question about downloading. How can I make Browsers to download straight the goods when someone is clicking on the download link.

Currently they need to right click on the link and "save link as"!

Hope this clear enough what I mean!

Please give me some ideas! Thank you.

Peter
#downloads
  • Profile picture of the author n7 Studios
    Typically I'd load another PHP page, typically in a hidden iframe, and set the page's header parameters to force a browser download (i.e. it shows the option in the browser to save / open the file).
    {{ DiscussionBoard.errors[1611714].message }}
    • Profile picture of the author mattalways
      Originally Posted by n7 Studios View Post

      Typically I'd load another PHP page, typically in a hidden iframe, and set the page's header parameters to force a browser download (i.e. it shows the option in the browser to save / open the file).
      That's what I do as well. From what I know, there is not an easier way to work around this. You can probably find a php download file script on the net then link to a download now page and have a hidden iframe to that script. I would probably do something like this:

      <iframe src="download_script.php?download=whatever.csv" style="display: none;">Upgrade your browser</iframe>

      So that Iframe will trigger the download, and the page the iframe is on will say something like you're downloading whatever file...
      Signature

      Quit wasting your money! If you need a website, get me to do it right! I'll probably even do it for less! Design/Development/Software, I'm your guy! matt@snidge.com
      {{ DiscussionBoard.errors[1611813].message }}
      • Profile picture of the author saschakimmel
        Sorry to correct you but there is an easier way, it's called the Content-Disposition header and I personally (please don't get this wrong) don't understand why you should use an iFrame at all.

        You can simply use a PHP script for that purpose.
        Here is one I quickly created from scratch based on a method I'm using on my websites.

        If you save it as e.g. dl.php a link would look like this:
        <a href="dl.php?file=info.zip">Download</a>

        The code assumes that the file to be downloaded is located in the same directory as the PHP file, if it's in a different directory you need to modifiy the $path variable accordingly.

        <?php

        // Using basename() to prevent downloading arbitrary content from the server
        $file = basename($_GET['file']);
        $path = dirname(__FILE__).'/'.$file;

        // Check if the file exists
        if (!file_exists($path) || !is_readable($path)) {
        header("HTTP/1.0 404 Not Found");
        die("<h1>404 File not found.</h1>");
        }

        // Content-type, PDF e.g. would use application/pdf
        header("Content-Type: application/octet-stream");

        // This tells the browser to download the file (attachment)
        header("Content-Disposition: attachment; filename=".$file);

        // This tells the browser that the content shouldn't be cached
        header('Expires: ' . gmdate('D, d M Y H:i:s', gmmktime() -3600) . ' GMT');
        header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
        header('Pragma: public');

        // Content-Length
        header("Content-Length: " . filesize($path));

        // Open the file to be downloaded and send it to the user
        $fp = fopen($path, "r");
        fpassthru($fp);

        ?>

        Hope this helps
        Signature

        ** Get my ViralListMachine software now for free and build your own list virally by giving away free stuff @ http://www.virallistmachinegiveaway.com **

        {{ DiscussionBoard.errors[1611865].message }}
        • Profile picture of the author n7 Studios
          Originally Posted by saschakimmel View Post

          Sorry to correct you but there is an easier way, it's called the Content-Disposition header and I personally (please don't get this wrong) don't understand why you should use an iFrame at all.
          Because when a user click the download link, you want to show them the download page, perhaps with instructions on what to do with the download, as well as force / prompt the download at the same time. Saves on customers / visitors asking questions about what to do with said download.
          {{ DiscussionBoard.errors[1614978].message }}
          • Profile picture of the author saschakimmel
            Ok, I see.

            I'd however use either a META REFRESH tag in the header of the page or a simple JavaScript like this:
            <script type="text/javascript">
            top.location.href='/download.php?file=...;
            </script>
            Signature

            ** Get my ViralListMachine software now for free and build your own list virally by giving away free stuff @ http://www.virallistmachinegiveaway.com **

            {{ DiscussionBoard.errors[1615019].message }}
  • Profile picture of the author mattalways
    I get browser problems with some browsers when using this method. That is why I started to use the iframe.
    Signature

    Quit wasting your money! If you need a website, get me to do it right! I'll probably even do it for less! Design/Development/Software, I'm your guy! matt@snidge.com
    {{ DiscussionBoard.errors[1612569].message }}
    • Profile picture of the author saschakimmel
      Well, this is a proven method used on sites that get millions of pageviews per month. Did you actually try my script?
      Signature

      ** Get my ViralListMachine software now for free and build your own list virally by giving away free stuff @ http://www.virallistmachinegiveaway.com **

      {{ DiscussionBoard.errors[1614020].message }}
  • Profile picture of the author mattalways
    I gave it a try. That is a nice script.

    I'm talking about with more of a force download though... where you go to the page that says it is downloading and it automatically pops up. I haven't been able to get that to work without putting it in an Iframe because without the Iframe some browsers just load the download script and don't print the page contents.
    Signature

    Quit wasting your money! If you need a website, get me to do it right! I'll probably even do it for less! Design/Development/Software, I'm your guy! matt@snidge.com
    {{ DiscussionBoard.errors[1614271].message }}
  • Profile picture of the author Peter Hupuczi
    I saying a Huge Thank YOU for All of You for your help!

    Forgive me, but I'm not techie and I have no idea to use this scripts. What I have done is cut and paste the code into an empty FrontPage page then saved. After I have renamed it to make it a .php file! So now i have a .php just no clue what should do next!
    {{ DiscussionBoard.errors[1614796].message }}
  • Profile picture of the author Peter Hupuczi
    I have Find it out! Everything is Working!

    Thank You Very Much!!!!
    {{ DiscussionBoard.errors[1614835].message }}

Trending Topics