call to method

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Duff B

    call to method

    Hi,
    this code perfectly works when I try to force the download of a file,
    by calling it directly.



    <?php

    $file = "onefilename.tx t";
    if (!is_file($file )) { die("<b>404 File not found!</b>"); }
    //
    $filename = basename($file) ;
    //
    header("Pragma: public");
    header("Expires : 0");
    header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
    header("Cache-Control: public");
    header("Content-Description: File Transfer");
    //
    $ctype="applica tion/force-download";
    header("Content-Type: $ctype");
    //
    $header="Conten t-Disposition: attachment; filename=".$fil ename.";";
    header($header );
    header("Content-Transfer-Encoding: binary");
    header("Content-Length: ".$len);
    @readfile($file );
    exit;

    ?>



    Now, I can't get this working as a method of a class:



    function downloadFile($t heFile){

    $file = $theFile;
    if (!is_file($file )) { die("<b>404 File not found!</b>"); }
    //
    $filename = basename($file) ;
    //
    header("Pragma: public");
    header("Expires : 0");
    header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
    header("Cache-Control: public");
    header("Content-Description: File Transfer");
    //
    $ctype="applica tion/force-download";
    header("Content-Type: $ctype");
    //
    $header="Conten t-Disposition: attachment; filename=".$fil ename.";";
    header($header );
    header("Content-Transfer-Encoding: binary");
    header("Content-Length: ".$len);
    @readfile($file );
    exit;
    }


    After instantiating the class, I call
    instance.downlo adFile("myFile. txt") but I get no prompt.

    What am I doing the wrong way?

    Thanks.

  • Sjoerd

    #2
    Re: call to method

    Use $instance->downloadFile() . The dot (.) is used for concatenating
    strings. The arrow (->) to call methods in classes.

    Comment

    • Steve

      #3
      Re: call to method

      [color=blue]
      > After instantiating the class, I call
      > instance.downlo adFile("myFile. txt") but I get no prompt.[/color]
      [color=blue]
      > What am I doing the wrong way?[/color]

      Erm, how about:

      instance->downloadFile(" myFile.txt");

      and turn error reporting on so PHP tells you all about your coding
      errors.

      ---
      Steve

      Comment

      • Steve

        #4
        Re: call to method


        That should be:

        $instance->downloadFile(" myFile.txt");
        [color=blue]
        > and turn error reporting on so PHP tells you all about your coding
        > errors.[/color]

        and I should practice what I preach 8-)

        ---
        Steve

        Comment

        • Duff B

          #5
          Re: call to method

          I beg your pardon:
          I wrote in JS manner what I did the way you suggest.

          I really don't understand why it doesn't work...

          Comment

          • Duff B

            #6
            Re: call to method

            Hi again,
            now it "perfectly" works...
            even if there's another perfectly strange thing:
            I get the prompt, I get the file downloaded BUT the file appears
            corrupted and with more bytes than the original (original: 5.75Kb,
            downloadedFile: 6.18Kb). Anybody knows what's happening today? :-)

            Cheers.



            Duff B ha scritto:
            [color=blue]
            > I beg your pardon:
            > I wrote in JS manner what I did the way you suggest.
            >
            > I really don't understand why it doesn't work...[/color]

            Comment

            • Steve

              #7
              Re: call to method

              [color=blue]
              > I get the prompt, I get the file downloaded BUT the file appears
              > corrupted and with more bytes than the original (original: 5.75Kb,
              > downloadedFile: 6.18Kb). Anybody knows what's happening today? :-)[/color]

              A couple of questions:

              Where does variable $len come from? It looks like it must be a global,
              since it isn't assigned in your method, so how is it asssigned?

              Are you sure that binary is the correct format for a text file? Could
              there be some unintended translations of CR/LF characters going on as a
              result?

              ---
              Steve

              Comment

              • Duff B

                #8
                Re: call to method

                Hi Steve, and thanks:
                $len came from a filesize, so by correcting it I got the file properly
                downloaded.





                [color=blue]
                > Are you sure that binary is the correct format for a text file? Could
                > there be some unintended translations of CR/LF characters going on as a
                > result?
                >
                > ---
                > Steve[/color]

                Comment

                Working...