[PHP]
if ($forceDownload ) { // HANDLE FORCED DOWNLOAD
$dlGen =& new DownloadGenerat or($fullFilePat h);
$negativeIndex = $dlGen->generateForceD ownloadHeaders( );
$willDeleteTemp = $dlGen->getSession('wi llDeleteTemp');
if (!$dlGen->isSuccessful ) $errorArray = $dlGen->getErrorArray( );
if ((int)$negative Index !== -1 && $willDeleteTemp ) {
if ($dlGen->getSession('al bum')) {
$locationPath = "${'tmp' .
ucfirst($dlGen->getSession('se ction')) . 'DownloadDir'}/" .
$dlGen->getSession('al bum');
} else {
$locationPath = "${'tmp' .
ucfirst($dlGen->getSession('se ction')) . 'DownloadDir'}" ;
}
@unlink("$locat ionPath/" . $dlGen->getSession('id '));
}
if (!headers_sent( ) && (int)$negativeI ndex !== -1)
header("Locatio n: index.php" . $dlGen->generateQueryS tring());
$dlGen = null;
}
[/PHP]
Using class DownloadGenerat or:
[PHP]
/**
* Download management
*
* @author Phil Powell
* @version 1.1.0
* @package APP
*/
class DownloadGenerat or extends MethodGenerator ForActionPerfor mer {
/**
* Full File Path
*
* @access private
* @var mixed $fullFilePath
*/
var $fullFilePath;
/**
* Constructor. Populate $fullFilePath property with parameter
*
* @access public
* @param mixed $fullFilePath
*/
function DownloadGenerat or($fullFilePat h) { // CONSTRUCTOR
$this->fullFilePath = $fullFilePath;
}
//-------------------------------------------------- --* GETTER/SETTER
METHODS *-- ------------------------------------------------
/**
* Retrieve $_SESSION variable array value by key name or entire array
if not referenced
*
* @access public
* @param mixed $key (optional)
* @return array
$_SESSION["${projectAcron ym}_forceDownlo adRetainVars"] (if no
parameter)
* @return mixed
$_SESSION["${projectAcron ym}_forceDownlo adRetainVars"][$key] (if
parameter)
*/
function &getSession($ke y = '') { // STATIC ARRAY OR MIXED METHOD
global $projectAcronym ;
if ($key) return
$_SESSION["${projectAcron ym}_forceDownlo adRetainVars"][$key];
return $_SESSION["${projectAcron ym}_forceDownlo adRetainVars"];
}
/**
* Set a $_SESSION variable 'forceDownloadR etainVars' that will
contain the key => $key reserved pairs
*
* @access public
* @param array $keyValArray
*/
function &setSession($ke yValArray) { // STATIC VOID METHOD
global $projectAcronym ;
if (is_array($keyV alArray) && @sizeof($keyVal Array) > 0)
$_SESSION["${projectAcron ym}_forceDownlo adRetainVars"] = $keyValArray;
}
//-------------------------------------------------- --* END OF
GETTER/SETTER METHODS *-- -------------------------------------
/**
* Generate header information to cause a forced download
*
* @access public
* @param mixed $fullFilePath (optional)
* @return int -1 - Use -1 as a flag to determine falsehood in this
case to distinguish from "false" which is equivalent in PHP to null,
the default return of this method
* @see actual_path
*/
function &generateForceD ownloadHeaders( $fullFilePath = '') { //
STATIC INT METHOD
$file = (is_object($thi s) && $this->fullFilePath ) ?
$this->fullFilePath : $fullFilePath;
if (is_file(actual _path($file))) {
$filesize = @filesize(actua l_path($file));
header('Content-Disposition: attachment; filename="' .
substr($file, strrpos($file, '/') + 1, strlen($file)) . '"');
header("Content-Length: $filesize");
header('Content-Type: application/octet-stream');
header('Content-Transfer-Encoding: binary');
header('Pragma: no-cache');
header('Expires : 0');
@readfile(actua l_path($file));
@set_time_limit (600);
} elseif (is_object($thi s)) {
$this->isSuccessful = false;
$this->setErrorArray( array('action' => "\"$file\" is not a valid
file"));
} else {
return -1;
}
}
/**
* Generate query string based upon values in
$_SESSION["${projectAcron ym}_forceDownlo adRetainVars"]
*
* @access public
* @return mixed query string
*/
function &generateQueryS tring() { // STATIC STRING METHOD
global $projectAcronym ;
if (is_array($_SES SION["${projectAcron ym}_forceDownlo adRetainVars"])
&& @sizeof($_SESSI ON["${projectAcron ym}_forceDownlo adRetainVars"]) > 0)
{
foreach ($_SESSION["${projectAcron ym}_forceDownlo adRetainVars"] as
$key => $val) $qs .= "&$key=" . urlencode($val) ;
$qs = '?' . substr($qs, 1, strlen($qs));
unset($_SESSION["${projectAcron ym}_forceDownlo adRetainVars"]);
}
return $qs;
}
/**
* Generate server-side redirection
*
* @access public
* @param mixed $fullFilePath (optional)
*/
function generateRedirec t($fullFilePath = '') { // VOID METHOD
$fullFilePath = ($this->fullFilePath ) ? $this->fullFilePath :
$fullFilePath;
if ($fullFilePath) header("Locatio n:
index.php?force Download=1&full FilePath=" . urlencode($full FilePath));
}
}
[/PHP]
This section of code works just fine in our Windows and Linux systems -
unless Apache has been recently updated as of last Friday, when,
suddenly, the forced download never occurs.
It seems as if the HTTP headers are either not sent, misconfigured or
ignored (do note the method generateForceDo wnloadHeaders() ).
Any ideas?
Thanx
Phil
if ($forceDownload ) { // HANDLE FORCED DOWNLOAD
$dlGen =& new DownloadGenerat or($fullFilePat h);
$negativeIndex = $dlGen->generateForceD ownloadHeaders( );
$willDeleteTemp = $dlGen->getSession('wi llDeleteTemp');
if (!$dlGen->isSuccessful ) $errorArray = $dlGen->getErrorArray( );
if ((int)$negative Index !== -1 && $willDeleteTemp ) {
if ($dlGen->getSession('al bum')) {
$locationPath = "${'tmp' .
ucfirst($dlGen->getSession('se ction')) . 'DownloadDir'}/" .
$dlGen->getSession('al bum');
} else {
$locationPath = "${'tmp' .
ucfirst($dlGen->getSession('se ction')) . 'DownloadDir'}" ;
}
@unlink("$locat ionPath/" . $dlGen->getSession('id '));
}
if (!headers_sent( ) && (int)$negativeI ndex !== -1)
header("Locatio n: index.php" . $dlGen->generateQueryS tring());
$dlGen = null;
}
[/PHP]
Using class DownloadGenerat or:
[PHP]
/**
* Download management
*
* @author Phil Powell
* @version 1.1.0
* @package APP
*/
class DownloadGenerat or extends MethodGenerator ForActionPerfor mer {
/**
* Full File Path
*
* @access private
* @var mixed $fullFilePath
*/
var $fullFilePath;
/**
* Constructor. Populate $fullFilePath property with parameter
*
* @access public
* @param mixed $fullFilePath
*/
function DownloadGenerat or($fullFilePat h) { // CONSTRUCTOR
$this->fullFilePath = $fullFilePath;
}
//-------------------------------------------------- --* GETTER/SETTER
METHODS *-- ------------------------------------------------
/**
* Retrieve $_SESSION variable array value by key name or entire array
if not referenced
*
* @access public
* @param mixed $key (optional)
* @return array
$_SESSION["${projectAcron ym}_forceDownlo adRetainVars"] (if no
parameter)
* @return mixed
$_SESSION["${projectAcron ym}_forceDownlo adRetainVars"][$key] (if
parameter)
*/
function &getSession($ke y = '') { // STATIC ARRAY OR MIXED METHOD
global $projectAcronym ;
if ($key) return
$_SESSION["${projectAcron ym}_forceDownlo adRetainVars"][$key];
return $_SESSION["${projectAcron ym}_forceDownlo adRetainVars"];
}
/**
* Set a $_SESSION variable 'forceDownloadR etainVars' that will
contain the key => $key reserved pairs
*
* @access public
* @param array $keyValArray
*/
function &setSession($ke yValArray) { // STATIC VOID METHOD
global $projectAcronym ;
if (is_array($keyV alArray) && @sizeof($keyVal Array) > 0)
$_SESSION["${projectAcron ym}_forceDownlo adRetainVars"] = $keyValArray;
}
//-------------------------------------------------- --* END OF
GETTER/SETTER METHODS *-- -------------------------------------
/**
* Generate header information to cause a forced download
*
* @access public
* @param mixed $fullFilePath (optional)
* @return int -1 - Use -1 as a flag to determine falsehood in this
case to distinguish from "false" which is equivalent in PHP to null,
the default return of this method
* @see actual_path
*/
function &generateForceD ownloadHeaders( $fullFilePath = '') { //
STATIC INT METHOD
$file = (is_object($thi s) && $this->fullFilePath ) ?
$this->fullFilePath : $fullFilePath;
if (is_file(actual _path($file))) {
$filesize = @filesize(actua l_path($file));
header('Content-Disposition: attachment; filename="' .
substr($file, strrpos($file, '/') + 1, strlen($file)) . '"');
header("Content-Length: $filesize");
header('Content-Type: application/octet-stream');
header('Content-Transfer-Encoding: binary');
header('Pragma: no-cache');
header('Expires : 0');
@readfile(actua l_path($file));
@set_time_limit (600);
} elseif (is_object($thi s)) {
$this->isSuccessful = false;
$this->setErrorArray( array('action' => "\"$file\" is not a valid
file"));
} else {
return -1;
}
}
/**
* Generate query string based upon values in
$_SESSION["${projectAcron ym}_forceDownlo adRetainVars"]
*
* @access public
* @return mixed query string
*/
function &generateQueryS tring() { // STATIC STRING METHOD
global $projectAcronym ;
if (is_array($_SES SION["${projectAcron ym}_forceDownlo adRetainVars"])
&& @sizeof($_SESSI ON["${projectAcron ym}_forceDownlo adRetainVars"]) > 0)
{
foreach ($_SESSION["${projectAcron ym}_forceDownlo adRetainVars"] as
$key => $val) $qs .= "&$key=" . urlencode($val) ;
$qs = '?' . substr($qs, 1, strlen($qs));
unset($_SESSION["${projectAcron ym}_forceDownlo adRetainVars"]);
}
return $qs;
}
/**
* Generate server-side redirection
*
* @access public
* @param mixed $fullFilePath (optional)
*/
function generateRedirec t($fullFilePath = '') { // VOID METHOD
$fullFilePath = ($this->fullFilePath ) ? $this->fullFilePath :
$fullFilePath;
if ($fullFilePath) header("Locatio n:
index.php?force Download=1&full FilePath=" . urlencode($full FilePath));
}
}
[/PHP]
This section of code works just fine in our Windows and Linux systems -
unless Apache has been recently updated as of last Friday, when,
suddenly, the forced download never occurs.
It seems as if the HTTP headers are either not sent, misconfigured or
ignored (do note the method generateForceDo wnloadHeaders() ).
Any ideas?
Thanx
Phil
Comment