You are here: Forced download fails in PHP 4.3+ w/ upgraded Apache 2.0.52 « PHP Programming Language « IT news, forums, messages
Forced download fails in PHP 4.3+ w/ upgraded Apache 2.0.52

Posted by comp.lang.php on 04/11/06 23:20

[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

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация