#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2013
    Posts
    4
    Rep Power
    0

    Force downloading several files - issues


    I have got stuck with downloading info sheets for our club.
    Everything is perfect including GUI, selections and payments until:
    PHP Code:
    while($n $requested) {
    .......
    code to generate $fi;  
    header('Content-disposition: attachment; filename='.$fi);
    header('Content-type: image/gif');
    readfile($fi);
    n++;
    Update other controls....;

    "Other controls" are updated correctly in the browser only if the header lines are commented out but *if* they are included then I get one (only) forced download but no browser update.
    I've tried fiddling around with $_SESSION but didn't succeed.
    Question 2: How do I get a series of (say 3) downloads to work within the while statement - can it be done sequentially or do I have to wrap them into one zip (not a good solution for our membership!)?
    Many thanks
  2. #2
  3. Wiser? Not exactly.
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    May 2001
    Location
    Bonita Springs, FL
    Posts
    5,905
    Rep Power
    3969
    Originally Posted by Hillybilly
    Question 2: How do I get a series of (say 3) downloads to work within the while statement - can it be done sequentially or do I have to wrap them into one zip (not a good solution for our membership!)?
    Many thanks
    One request can only return one file. As a result, if you want to download several files, you must first combine them in something like a ZIP or TAR file, then download that.
    Recycle your old CD's, don't just trash them



    If I helped you out, show some love with some reputation, or tip with Bitcoins to 1N645HfYf63UbcvxajLKiSKpYHAq2Zxud
  4. #3
  5. No Profile Picture
    Dazed&Confused
    Devshed Novice (500 - 999 posts)

    Join Date
    Jun 2002
    Location
    Tempe, AZ
    Posts
    501
    Rep Power
    127
    I haven't tested it but maybe you could accomplish this via Javascript by doing:

    Code:
    document.location = "file1";
    document.location = "file2";
    document.location = "file3";
    I've noticed in the past that Javascript will continue to run even if a page reload has been initiated, so long as the browser isn't actually leaving the page. If your headers for the downloads are right, you shouldn't leave the page, allowing the JS to go through the whole list.

    But as I said, this isn't tested. And it's possible some browsers might handle this differently than others.
    LinkedIn: Dave Mittner
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2013
    Posts
    4
    Rep Power
    0

    Force downloading several files - issues


    Originally Posted by kicken
    One request can only return one file. As a result, if you want to download several files, you must first combine them in something like a ZIP or TAR file, then download that.
    Thanks for this info . Not what I wanted to hear but it makes sense. I will have to think about a workaround.
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2013
    Posts
    4
    Rep Power
    0

    Smile Force downloading several files - issues


    Originally Posted by dmittner
    I haven't tested it but maybe you could accomplish this via Javascript by doing:

    Code:
    document.location = "file1";
    document.location = "file2";
    document.location = "file3";
    I've noticed in the past that Javascript will continue to run even if a page reload has been initiated, so long as the browser isn't actually leaving the page. If your headers for the downloads are right, you shouldn't leave the page, allowing the JS to go through the whole list.

    But as I said, this isn't tested. And it's possible some browsers might handle this differently than others.
    Thank you. I will try it but it will have to wait for a few days.
  10. #6
  11. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2009
    Location
    Jakarta, Indonesia.
    Posts
    184
    Rep Power
    31
    The idea of putting the loop in session is tempting. Your user however, would be surprised (or confused?) to see 3 download popups be shown simultaneously on his/her browser...

    I think this would be nicer (and probably easier for you) if a special page could be created specially for the downloadables. Your clients should have the freedom to choose which should be downloaded first or later. No?
  12. #7
  13. No Profile Picture
    Dazed&Confused
    Devshed Novice (500 - 999 posts)

    Join Date
    Jun 2002
    Location
    Tempe, AZ
    Posts
    501
    Rep Power
    127
    Originally Posted by hdewantara
    The idea of putting the loop in session is tempting.
    How would that work?
    A single PHP page load can only do one thing: either kick-off a download or serve a new page/redirect. I'm not sure how your loop would operate...
    LinkedIn: Dave Mittner
  14. #8
  15. No Profile Picture
    Contributing User
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Dec 2004
    Posts
    2,861
    Rep Power
    368
    Originally Posted by hdewantara
    The idea of putting the loop in session is tempting. Your user however, would be surprised (or confused?) to see 3 download popups be shown simultaneously on his/her browser...

    I think this would be nicer (and probably easier for you) if a special page could be created specially for the downloadables. Your clients should have the freedom to choose which should be downloaded first or later. No?
    I agree, great idea If however these files become "ready". You should have a page or a "pop up, jquery modal window" which has link to each file. The user can then download each file..
  16. #9
  17. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2013
    Posts
    4
    Rep Power
    0

    Smile


    Originally Posted by dmittner
    How would that work?
    A single PHP page load can only do one thing: either kick-off a download or serve a new page/redirect. I'm not sure how your loop would operate...
    Thanks all/
    I will work on these ideas.
    I'm not sure that the javascript one [document.location = "file3";] will work because I've seen references in the documentation to it being both 'read only' *and* 'r/w' ..... err..umm. A quick try suggests its 'read only'.
    If I get this sorted I will post my solution.
    Re Q1: any ideas why the code does not seem to flow through? Do the new header statements then block the existing page from being updated? Would a session statement capture this to enable a reload?
    Appreciated all your inputs.
  18. #10
  19. No Profile Picture
    Dazed&Confused
    Devshed Novice (500 - 999 posts)

    Join Date
    Jun 2002
    Location
    Tempe, AZ
    Posts
    501
    Rep Power
    127
    I was able to get the concept working, at least in Chrome, with this:

    Code:
    <script>
    function download(){
      document.location = 'file1.zip';
      setTimeout(function(){document.location = 'file2.zip';},1000);
      setTimeout(function(){document.location = 'file3.zip';},2000);
    }
    </script>
    <input type="button" value="Download" onclick="download();"/>
    I had to introduce a delay between the file kick-offs else it'd just immediately skip to doing the 3rd one. It might be a bit more reliable to kick off 3 new popup windows quickly to initiate the downloads in, but that route lacks a bit of grace.
    LinkedIn: Dave Mittner

IMN logo majestic logo threadwatch logo seochat tools logo