473,325 Members | 2,785 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,325 software developers and data experts.

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

[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
2 1408
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

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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: JT | last post by:
I'm running Redhat 9 and just upgraded from PHP 4.2.x to 4.3.1 and no when I try to execute a script on my local server here it opens a window to try and download any .php files. I think Apache...
1
by: dany | last post by:
I want a forced PDF-download but all that header-stuff is new for me.... I wrote a little asp-script: <% Filename = "download.pdf" response.addheader "content-disposition","attachment;...
2
by: Gary D. Rezek | last post by:
Hi All, First of all I'm mostly an MS Access guy (and novice learning MSSQL guy) and put on this project because of personnel changes, etc and because of db experience. I've only been doing this...
2
by: mike | last post by:
I've spent a couple of days on this and I'm to the "bang-my-head 'gainst the monitor and babble in tongues" mode. First - SuSE Pro 9.3, Linux 2.6.11.4-21.7, Apache 2.0.53, PHP 4.3.10 I have...
18
by: jacksu | last post by:
I have a simple program to run xpath with xerces 1_2_7 XPathFactory factory = XPathFactory.newInstance(); XPath xPath = factory.newXPath(); XPathExpression xp = xPath.compile(strXpr);...
14
by: Simon Rigby | last post by:
Hi folks, I appreciate that this topic of forced downloads has been discussed in various threads but my situtation is a little different to those that I have come across and am experiencing a...
12
by: comp.lang.php | last post by:
index.php: // STUFF // STEP 1: imagecreatetruecolor ONLY IF GD 2.0+ SUPPORTED AND FOUND if ($this->isSuccessful && !$hasMogrified && $image && !$newImage &&...
12
by: David T. Ashley | last post by:
I've noticed that some scripting languages (PHP, for example) have options to control whether the script can be terminated by the user clicking STOP on their browser (or similar mechanisms). How...
2
by: yord | last post by:
Hello, I have a PHP based site running for 2 years without any problems. The user can upload images and I run the is_uploaded_file() method to check if the file was really uploaded via HTTP...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.