NYCPHP Meetup

NYPHP.org

[nycphp-talk] How do I use readfile() and redirect at the same time?

Phillip Powell phillip.powell at adnet-sys.com
Wed May 12 14:35:12 EDT 2004


[PHP]
class DownloadGenerator {
 
  var $fullFilePath;

  function DownloadGenerator($fullFilePath) {
   $this->fullFilePath = $fullFilePath;
  }

  function &generateForceDownloadHeaders($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 (that obviously fails) was this:
[PHP]
  foreach ($_REQUEST as $key => $val) if (!isset(${$key})) ${$key} = 
$val;        // RETRIEVE ALL PASSED VARS
   
    if ($forceDownload) {                                                
    // HANDLE FORCED DOWNLOAD
     $dlGen =& new DownloadGenerator($fullFilePath);
     $dlGen->generateForceDownloadHeaders();
     echo "<a href=\"index.php" . $dlGen->generateQueryString() . 
"\">Continue</a>\n";
     $dlGen = null;
    }
[/PHP]

This resulting from 
[Quote]index.php?forceDownload=1&fullFilePath=/tmp/images/blah.jpg[/Quote]

Thanx
Phil

-- 
---------------------------------------------------------------------------------
Phil Powell
Multimedia Programmer
ADNET Systems., Inc.
11260 Roger Bacon Drive Suite 403
Reston, VA  20191
#: (703) 709-7218 x107 
Fax: (703) 709-7219

	




More information about the talk mailing list