readfile() and redirection

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Phil Powell

    readfile() and redirection

    [PHP]
    class DownloadGenerat or {

    var $fullFilePath;

    function DownloadGenerat or($fullFilePat h) {
    $this->fullFilePath = $fullFilePath;
    }

    function &generateForceD ownloadHeaders( $fullFilePath = '') { //
    STATIC VOID METHOD
    $file = ($this->fullFilePath ) ? $this->fullFilePath : $fullFilePath;
    if (if_file($file) ) {
    $filesize = @filesize($file );
    header("Content-Disposition: attachment; filename=$file" );
    header('Content-Type: application/octet-stream');
    header('Content-Type: application/force-download');
    header('Content-Type: application/download');
    header('Content-Transfer-Encoding: binary');
    header('Pragma: no-cache');
    header('Expires : 0');
    @set_time_limit (600);
    readfile($file) ;
    }
    }

    }
    [/PHP]

    I am trying to "have my cake and eat it too", so to speak. I want to
    do a forced download of a file using readfile() and manipulating the
    headers; but at the exact same time I need to redirect to another
    page, the page that says "hey everything is cool" success message and
    display the rest of the application.

    Right now what it does is that it does force a download (in Mozilla)
    but your window is blank. The user has no ability to navigate any
    further and is forced to close and re-open their browser and start all
    over again, definitely not an option.

    How can I use readfile() to force a download of a file, yet at the
    same time display a screen indicating the rest of the application?

    The idea I thought up was this:
    [PHP]
    foreach ($_REQUEST as $key => $val) if (!isset(${$key} )) ${$key} =
    $val; // RETRIEVE ALL PASSED VARS

    if ($forceDownload ) { // HANDLE FORCED DOWNLOAD
    $dlGen =& new DownloadGenerat or($fullFilePat h);
    $dlGen->generateForceD ownloadHeaders( );
    echo "<a href=\"index.ph p" . $dlGen->generateQueryS tring() .
    "\">Continu e</a>\n";
    $dlGen = null;
    }
    [/PHP]

    This resulting from
    index.php?force Download=1&full FilePath=/tmp/images/blah.jpg
    Thanx
    Phil
  • Chung Leong

    #2
    Re: readfile() and redirection

    > I am trying to "have my cake and eat it too", so to speak. I want to[color=blue]
    > do a forced download of a file using readfile() and manipulating the
    > headers; but at the exact same time I need to redirect to another
    > page, the page that says "hey everything is cool" success message and
    > display the rest of the application.[/color]

    Easy. Use an invisible frame to initiate the download.


    Comment

    • Phil Powell

      #3
      Re: readfile() and redirection

      "Chung Leong" <chernyshevsky@ hotmail.com> wrote in message news:<K8OdnQ6iE Zv_Pj_dRVn-jw@comcast.com> ...[color=blue][color=green]
      > > I am trying to "have my cake and eat it too", so to speak. I want to
      > > do a forced download of a file using readfile() and manipulating the
      > > headers; but at the exact same time I need to redirect to another
      > > page, the page that says "hey everything is cool" success message and
      > > display the rest of the application.[/color]
      >
      > Easy. Use an invisible frame to initiate the download.[/color]

      I'm sorry. Invisible frame? <Iframe>? I don't understand, I don't
      follow your logic, I'm sorry.

      What I have is that upon resizing the image it will do a header()
      redirection to a URL that will have its headers changed using a class
      method which allows for your browser window to remain in place while
      the download prompt comes up via your browser to download the file.
      After file download takes place you're still where you are and files
      are managed.

      I am interested in your easy solution, as you say, but can't
      comprehend it in light of the modular approach I have done with the
      application I've done so far. Please do explain "invisible frame" to
      me.

      Thanx
      Phil

      Comment

      • Chung Leong

        #4
        Re: readfile() and redirection

        "Phil Powell" <soazine@erols. com> wrote in message
        news:1cdca2a7.0 405121926.79b2b f74@posting.goo gle.com...[color=blue]
        > What I have is that upon resizing the image it will do a header()
        > redirection to a URL that will have its headers changed using a class
        > method which allows for your browser window to remain in place while
        > the download prompt comes up via your browser to download the file.
        > After file download takes place you're still where you are and files
        > are managed.[/color]

        It's very simple really. You want to both redirect to a HTML page and
        initiate a download. We're talking two HTTP requests here. And the only way
        you can initiate multiple page requests to the server with a single click
        (without resorting to Javascript) is through frames.

        In the page that you redirect to, stick in something like:

        <iframe src="download.p hp?param=someth ing" style="display: none" />

        That will bring up a download box if the appropriate headers are set. In the
        event of problems, the download script can output Javascript code that
        either shows the errors on the parent page
        (parent.documen t.getElementsBy Id(...).innerHT ML = '' ...) or redirect the
        parent to an error page. An alternate strategy would have the page perform
        the resizing, save the result to a temp file, which the download script
        would then output and delete.


        Comment

        Working...