SitePoint Sponsor

User Tag List

Results 1 to 11 of 11
  1. #1
    SitePoint Addict
    Join Date
    Sep 2008
    Posts
    341
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    File download and $_SESSION wont work together

    This works: (file download)
    Code:
    //session_start();
    $fileurl = "/home/account/folder1/folder2/file.xlsx";
    $file= "file.xlsx";
    header("Content-Disposition: attachment; filename=$file");
    readfile($fileurl);
    This does not:
    Code:
    session_start();
    $fileurl = "/home/account/folder1/folder2/file.xlsx";
    $file= "file.xlsx";
    header("Content-Disposition: attachment; filename=$file");
    readfile($fileurl);
    Why? I must use sessions on the page.

  2. #2
    SitePoint Evangelist
    Join Date
    May 2006
    Location
    Denmark
    Posts
    407
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Define "doesn't work", please

  3. #3
    SitePoint Enthusiast
    Join Date
    Sep 2006
    Posts
    76
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I don't see any reason it wouldn't work. Maybe having the session set is changing something else that is throwing it off?

    Any error codes?

  4. #4
    SitePoint Addict
    Join Date
    Sep 2008
    Posts
    341
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    This is the only code on the page. What I have posted is NOT just a snippet. (just for info, I do need session_start but I have stripped everything out to ensure it is not something else.)

    Without session_start it downloads as you would expect.

    With session start it it says "Internet Explorer cannot download the file - it was not able to open the site"

    I have also tried session_write_close(); just before the actual download - but, no.

    Please try on your own system, I am sure you will agree this is crazy.

  5. #5
    SitePoint Evangelist
    Join Date
    May 2006
    Location
    Denmark
    Posts
    407
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Looks like you aren't sending a Content-type header. If you don't then it'll be text/html by default so that might be the problem.

  6. #6
    SitePoint Addict
    Join Date
    Sep 2008
    Posts
    341
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'll try this.

    Any idea what the content type is for xlsx ?

  7. #7
    SitePoint Enthusiast
    Join Date
    Sep 2006
    Posts
    76
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    What browser are you using? Is the site using SSL?

    Try adding:

    PHP Code:
    session_cache_limiter('public'); 
    Before your session_start(); call

  8. #8
    SitePoint Addict
    Join Date
    Sep 2008
    Posts
    341
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Daniel0's solution:
    I have looked at http://www.webmaster-toolkit.com/mime-types.shtml
    tried: header("Content-Type: application/x-excel");
    but it still shows the same problem

    Brad080283's solution:
    IE7 (although must work on all) / SSL=yes

    YES!!!!!!!!!!!!!!!!!!!!!!!!!

    Brad wins - I have tried PHP freaks (phpfreaks.com), and nobody knows the answer, so well done!!!

    Thanks also for your help Daniel - appreciate from everyone.

  9. #9
    SitePoint Evangelist
    Join Date
    May 2006
    Location
    Denmark
    Posts
    407
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by johnsmith153 View Post
    I'll try this.

    Any idea what the content type is for xlsx ?
    application/vnd.openxmlformats-officedocument.spreadsheetml.sheet

    You could also try: application/force-download

  10. #10
    SitePoint Addict
    Join Date
    Sep 2008
    Posts
    341
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    -

  11. #11
    SitePoint Evangelist
    Join Date
    May 2006
    Location
    Denmark
    Posts
    407
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ah... lesson learned: Provide as much information about your environment and script as possible


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •