PHP Gurus

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • qw12er
    Confirmed User
    • Apr 2004
    • 799

    #1

    PHP Gurus

    I'm using this function to send a file to my users :

    function send_file($name) {
    ob_end_clean();
    $path = $name;

    if (!is_file($path) or connection_status()!=0) return(FALSE);
    header("Cache-Control: no-store, no-cache, must-revalidate");
    header("Cache-Control: post-check=0, pre-check=0", false);
    header("Pragma: no-cache");
    header("Expires: ".gmdate("D, d M Y H:i:s", mktime(date("H")+2, date("i"), date("s"), date("m"), date("d"), date("Y")))." GMT");
    header("Last-Modified: ".gmdate("D, d M Y H:i:s")." GMT");
    header("Content-Type: application/octet-stream");
    header("Content-Length: ".(string)(filesize($path)));
    header("Content-Disposition: inline; filename=$name");
    header("Content-Transfer-Encoding: binary\n");

    if ($file = fopen($path, 'rb')) {
    while(!feof($file) and (connection_status()= =0)) {
    print(fread($file, 1024*8));
    ob_flush();
    }
    fclose($file);
    }
    return((connection_status()= =0) and !connection_aborted());
    }


    But for some reason if I try to open it without saving it on my disk it will causes an error. (Invalid Zip file) But if I save the file first and then open it everything works just fine.

    How can I fix that ?
    I have nothing to advertise ... yet.
  • Damian_Maxcash
    So Fucking Banned
    • Oct 2002
    • 12745

    #2
    Originally posted by qw12er
    I'm using this function to send a file to my users :

    function send_file($name) {
    ob_end_clean();
    $path = $name;

    if (!is_file($path) or connection_status()!=0) return(FALSE);
    header("Cache-Control: no-store, no-cache, must-revalidate");
    header("Cache-Control: post-check=0, pre-check=0", false);
    header("Pragma: no-cache");
    header("Expires: ".gmdate("D, d M Y H:i:s", mktime(date("H")+2, date("i"), date("s"), date("m"), date("d"), date("Y")))." GMT");
    header("Last-Modified: ".gmdate("D, d M Y H:i:s")." GMT");
    header("Content-Type: application/octet-stream");
    header("Content-Length: ".(string)(filesize($path)));
    header("Content-Disposition: inline; filename=$name");
    header("Content-Transfer-Encoding: binary\n");

    if ($file = fopen($path, 'rb')) {
    while(!feof($file) and (connection_status()= =0)) {
    print(fread($file, 1024*8));
    ob_flush();
    }
    fclose($file);
    }
    return((connection_status()= =0) and !connection_aborted());
    }


    But for some reason if I try to open it without saving it on my disk it will causes an error. (Invalid Zip file) But if I save the file first and then open it everything works just fine.

    How can I fix that ?

    Im not totally sure - but I think it could be the mime type setting in your apache config file that is the issue. That could be a start for you to Google from - but I could be way off.

    Comment

    • StuartD
      Sofa King Band
      • Jul 2002
      • 29903

      #3
      Originally posted by qw12er
      header("Content-Type: application/octet-stream");
      Try changing this to

      header("Content-Type: application/zip");

      A list of other content types can be found just over half way down the page here:
      http://www.expertsrt.com/tutorials/M...P_headers.html
      This is me on facebook
      This is me on twitter

      Comment

      • qw12er
        Confirmed User
        • Apr 2004
        • 799

        #4
        Was a nice Idea but it doesn't works either :-(
        I have nothing to advertise ... yet.

        Comment

        • Ace_luffy
          www.creationcrew.com
          • Feb 2005
          • 12164

          #5
          bump.....


          ++ Adult and Mainstream Websites Designs | 10 banners for only $50 | html5 Banners ++
          email : [email protected] Telegram : https://t.me/creationcrew WhatsApp : +63 956 420 4819 | HTML5/Responsive Site - Div/CSS - ElevatedX - NATs - Wordpress

          Comment

          • duckduckgoose
            Registered User
            • Mar 2006
            • 82

            #6
            If I open a zip file when prompted by my browser during testing (rather than save it), it opens fine for me (using WinRAR as my archiver on my pc here). The same zip file also opens fine if I actually save it to disk when prompted by the browser. This is using Firefox v1.5.0.4. The code does not work properly at all for me in IE6, but rather gives a browser error after trying to send the page (whatever.php where your function is sitting) itself.

            My guess is that your specific combination of headers used so as not to cache the file being downloaded are the problem for you. You're telling the browser not to cache a copy of the file, which is fine if you're saving it to disk locally. However, if you try and have the browser just open the file without having cached it, it will probably choke depending on browser behaviour. There are varying combinations of http headers you can use in your PHP scripts to avoid having files be cached on the local browser; some of them are browser specific (IE one way, most other browsers another way), so on past scripts i can remember doing a check for IE before sending appropriate headers for tools like yours.

            Read through the PHP documentation on headers for more info :
            http://ca3.php.net/header
            rRhino.com ...social networking for book fans...

            Comment

            • qw12er
              Confirmed User
              • Apr 2004
              • 799

              #7
              Your right ! it seems to works fine with FireFox.

              It try a lot of cache-related header but I can't seem to get IE to works. I just don't get it !
              I have nothing to advertise ... yet.

              Comment

              • mrthumbs
                salad tossing sig guy
                • Apr 2002
                • 11702

                #8
                why not a simple redirect to a physical file location?

                Comment

                • qw12er
                  Confirmed User
                  • Apr 2004
                  • 799

                  #9
                  Originally posted by mrthumbs
                  why not a simple redirect to a physical file location?
                  because I want to delete the file as soon as it has been downloaded.
                  Impossible to do if you redirect to the file...
                  I have nothing to advertise ... yet.

                  Comment

                  • Big_Red
                    Confirmed User
                    • Jun 2006
                    • 4147

                    #10
                    bump. php pro's time to step up! good luck qw12er.
                    60% Revshare.
                    http://www.boobycash.com We got the boobs and the cash!
                    ICQ 198-580-197 24/7 support

                    Comment

                    • qw12er
                      Confirmed User
                      • Apr 2004
                      • 799

                      #11
                      thanks for all the bump.

                      bump for myself :-)
                      I have nothing to advertise ... yet.

                      Comment

                      • qw12er
                        Confirmed User
                        • Apr 2004
                        • 799

                        #12
                        bump
                        5678
                        I have nothing to advertise ... yet.

                        Comment

                        • qw12er
                          Confirmed User
                          • Apr 2004
                          • 799

                          #13
                          Sorry to bump this an other time but I realy need a solution :-(
                          I have nothing to advertise ... yet.

                          Comment

                          • Damian_Maxcash
                            So Fucking Banned
                            • Oct 2002
                            • 12745

                            #14
                            Originally posted by qw12er
                            Sorry to bump this an other time but I realy need a solution :-(
                            Did you check out the mime type thing?

                            The more I think about it the I think that is the issue.

                            If you dont have access to your Apache configs then try this in a .htaccess file

                            AddType zip file zip

                            Comment

                            • BigBen
                              Confirmed User
                              • Nov 2004
                              • 2299

                              #15
                              Originally posted by qw12er
                              Sorry to bump this an other time but I realy need a solution :-(
                              Try the forums at devshed or webmasterworld.

                              Comment

                              • duckduckgoose
                                Registered User
                                • Mar 2006
                                • 82

                                #16
                                If you replace all of the header() lines you used with the following ones, it works correctly in IE6 and Firefox.

                                header('Cache-Control: ');
                                header('Pragma: ');
                                header("Content-Type: application/force-download");
                                header("Content-length: " . (string)(filesize($path)));
                                header("Content-Disposition: attachment; filename=" . $path);


                                Test it yourself and see how it works out for you.
                                Last edited by duckduckgoose; 07-16-2006, 04:05 PM.
                                rRhino.com ...social networking for book fans...

                                Comment

                                • qw12er
                                  Confirmed User
                                  • Apr 2004
                                  • 799

                                  #17
                                  I don't understand why but it does works !!!

                                  Thanks DuckDuckGoose.
                                  I have nothing to advertise ... yet.

                                  Comment

                                  Working...