By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,485 Members | 1,061 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,485 IT Pros & Developers. It's quick & easy.

Forced download fails in PHP 4.3+ w/ upgraded Apache 2.0.52

P: n/a
[PHP]
if ($forceDownload) { // HANDLE FORCED DOWNLOAD
$dlGen =& new DownloadGenerator($fullFilePath);
$negativeIndex = $dlGen->generateForceDownloadHeaders();
$willDeleteTemp = $dlGen->getSession('willDeleteTemp');
if (!$dlGen->isSuccessful) $errorArray = $dlGen->getErrorArray();
if ((int)$negativeIndex !== -1 && $willDeleteTemp) {
if ($dlGen->getSession('album')) {
$locationPath = "${'tmp' .
ucfirst($dlGen->getSession('section')) . 'DownloadDir'}/" .
$dlGen->getSession('album');
} else {
$locationPath = "${'tmp' .
ucfirst($dlGen->getSession('section')) . 'DownloadDir'}";
}
@unlink("$locationPath/" . $dlGen->getSession('id'));
}
if (!headers_sent() && (int)$negativeIndex !== -1)
header("Location: index.php" . $dlGen->generateQueryString());
$dlGen = null;

}
[/PHP]

Using class DownloadGenerator:

[PHP]
/**
* Download management
*
* @author Phil Powell
* @version 1.1.0
* @package APP
*/
class DownloadGenerator extends MethodGeneratorForActionPerformer {

/**
* Full File Path
*
* @access private
* @var mixed $fullFilePath
*/
var $fullFilePath;

/**
* Constructor. Populate $fullFilePath property with parameter
*
* @access public
* @param mixed $fullFilePath
*/
function DownloadGenerator($fullFilePath) { // 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["${projectAcronym}_forceDownloadRetainVars"] (if no
parameter)
* @return mixed
$_SESSION["${projectAcronym}_forceDownloadRetainVars"][$key] (if
parameter)
*/
function &getSession($key = '') { // STATIC ARRAY OR MIXED METHOD
global $projectAcronym;
if ($key) return
$_SESSION["${projectAcronym}_forceDownloadRetainVars"][$key];
return $_SESSION["${projectAcronym}_forceDownloadRetainVars"];
}

/**
* Set a $_SESSION variable 'forceDownloadRetainVars' that will
contain the key => $key reserved pairs
*
* @access public
* @param array $keyValArray
*/
function &setSession($keyValArray) { // STATIC VOID METHOD
global $projectAcronym;
if (is_array($keyValArray) && @sizeof($keyValArray) > 0)
$_SESSION["${projectAcronym}_forceDownloadRetainVars"] = $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 &generateForceDownloadHeaders($fullFilePath = '') { //
STATIC INT METHOD
$file = (is_object($this) && $this->fullFilePath) ?
$this->fullFilePath : $fullFilePath;
if (is_file(actual_path($file))) {
$filesize = @filesize(actual_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(actual_path($file));
@set_time_limit(600);
} elseif (is_object($this)) {
$this->isSuccessful = false;
$this->setErrorArray(array('action' => "\"$file\" is not a valid
file"));
} else {
return -1;
}
}

/**
* Generate query string based upon values in
$_SESSION["${projectAcronym}_forceDownloadRetainVars"]
*
* @access public
* @return mixed query string
*/
function &generateQueryString() { // STATIC STRING METHOD
global $projectAcronym;
if (is_array($_SESSION["${projectAcronym}_forceDownloadRetainVars"])
&& @sizeof($_SESSION["${projectAcronym}_forceDownloadRetainVars"]) > 0)
{
foreach ($_SESSION["${projectAcronym}_forceDownloadRetainVars"] as
$key => $val) $qs .= "&$key=" . urlencode($val);
$qs = '?' . substr($qs, 1, strlen($qs));
unset($_SESSION["${projectAcronym}_forceDownloadRetainVars"]);
}
return $qs;
}

/**
* Generate server-side redirection
*
* @access public
* @param mixed $fullFilePath (optional)
*/
function generateRedirect($fullFilePath = '') { // VOID METHOD
$fullFilePath = ($this->fullFilePath) ? $this->fullFilePath :
$fullFilePath;
if ($fullFilePath) header("Location:
index.php?forceDownload=1&fullFilePath=" . urlencode($fullFilePath));
}
}
[/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 generateForceDownloadHeaders() ).

Any ideas?

Thanx
Phil

Apr 11 '06 #1
Share this Question
Share on Google+
2 Replies


P: n/a
Before I look at the code...
If it only changed on an apache change then would it not be something
to do with the apache config?

Apr 11 '06 #2

P: n/a

fletch wrote:
Before I look at the code...
If it only changed on an apache change then would it not be something
to do with the apache config?


I'm trying to see if it's something I can fix on my end that will work
regardless of the apache config - the web application I built is built
to be portable and cross-platform

Phil

Apr 12 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.