|
Posted by comp.lang.php on 11/19/04 11:45
I have a form that when you click the "Generate Report" submit button,
it will force download a CSV file, required for this project.
On the very same page you also have a "Search" submit button, when you
press it it should generate search results in a new page.
However, when you click the "Generate Report" submit button, the moment
you try to THEN click the "Search" submit button, the "Search" submit
button NEVER goes to a new page but instead tries to force-download the
very same CSV file, as if the "Search" submit button literally becomes
the "Generate Report" submit button!
Is there a way using PHP and HTTP headers to force BOTH a download AND
a redirect literally at the exact same time? This is the only way I
can think of to stop this from happening, unless the user physically
reloads their page each time, which is not going to be acceptable by
the client.
[PHP]
class ActionPerformer extends DBActionPerformer {
var $id;
function ActionPerformer($id) {
$this->id = $id;
}
/**
* Generate a report
*
* @access protected
* @see change_first_order_flag
* @see actual_path
*/
function &report() { // STATIC VOID METHOD
global $scriptPath, $adminReportPath, $tempAdminReportPath;
$orderBy = ' upper(school_type_name) asc, upper(school_name) asc,
upper(student_last_name) asc, upper(student_first_name) asc,
upper(student_mi) asc ';
// BOOLEAN TO INDICATE IF A MOVE FROM A TEMP FOLDER TO THE ACTUAL
FOLDER WILL TAKE PLACE, ELSE, WILL REMAIN IN ACTUAL FOLDER
$willMoveReportFromTemp = ($tempAdminReportPath &&
strcmp(trim($adminReportPath), trim($tempAdminReportPath)) != 0);
// CHANGE "asc" TO "desc" AND VICE VERSA
if ($_REQUEST['willDesc']) $orderBy =
change_first_order_flag($orderBy);
// SUB OUT ALL CARRIAGE RETURNS AND LINE FEEDS
$orderBy = str_replace("\n", ' ', str_replace("\r", ' ',
htmlspecialchars($orderBy))); // EXTRA PROTECTION
$msg = "php -q \"" .
actual_path("$scriptPath/stored_procedures/get_student_group_resultset.php")
.. "\" \"ORDER BY $orderBy\" 'willExcludeCustomer' '' '' ''
'willGenerateReport'";
$reportFileName = exec($msg); // ERROR REDIRECTION HANDLED
BY PSEUDO-STORED PROCEDURE
if ($willMoveReportFromTemp) { // GENERATE RENAMED PATH +
FILENAME HERE FOR EFFICIENCY
$newReportFileName = preg_replace('/(^.*)' . str_replace('/', '\\/',
preg_quote($tempAdminReportPath)) . '(.*$)/', '$1' . $adminReportPath .
'$2', $reportFileName);
} else {
$newReportFileName = $reportFileName;
}
if (preg_match('/(error)|(errcode:?)/i', $reportFileName)) { //
ERROR ATTEMPTING TO TRY TO USE PSEUDO-STORED PROC TO SPAWN REPORT FILE
$this->isSuccessful = false;
$this->setErrorArray(array('action' => 'Unable to generate report: '
.. nl2br(htmlspecialchars($reportFileName)) . ': please contact
administrator'));
} else {
if ($willMoveReportFromTemp &&
@!copy(actual_path(realpath($reportFileName)),
actual_path($newReportFileName))) { // COPY FROM TEMP FOLDER IF
REQUIRED
list($copyKommand, $copyRedirect) =
@array_values($this->getKommandOSArray('copy'));
$msg = exec("$copyKommand \"" .
actual_path(realpath($reportFileName)) . '" "' .
actual_path($newReportFileName) . "\" $copyRedirect");
if ($msg || !is_file(actual_path($newReportFileName))) {
$errorMsg = "Unable to copy report from \"$reportFileName\" to
\"$newReportFileName\"";
if ($msg) $errorMsg .= ': "' . nl2br(htmlspecialchars($msg)) .
'"'; else echo ": No such file \"$newReportFileName\"";
$this->isSuccessful = false;
$this->setErrorArray(array('action' => $errorMsg));
}
}
// REAL IMPORTANT!!! CHMOD OR THE WORLD CAN SEE YOUR REPORTS!!!!!!!
if ($this->isSuccessful) @chmod(0770,
actual_path($newReportFileName)); // CHANGE PERMISSIONS (IF IN UNIX)
TO PREVENT WORLD FROM ACCESSING FILE
if ($this->isSuccessful)
ReportGenerator::generateHTTPHeaders($reportFileName); // THIS CAUSES
THE FORCED DOWNLOAD
@unlink(actual_path($newReportFileName)); // FILE HAS BEEN
FORCE-DOWNLOADED AND IS NO LONGER NEEDED ON SERVER
}
}
}
[/PHP]
Thanx
Phil
Navigation:
[Reply to this message]
|