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

Force download + Force redirect at the exact same time?

P: n/a
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($reportFileNa me); // THIS CAUSES
THE FORCED DOWNLOAD
@unlink(actual_path($newReportFileName)); // FILE HAS BEEN
FORCE-DOWNLOADED AND IS NO LONGER NEEDED ON SERVER
}
}

}
[/PHP]

Thanx
Phil

Apr 20 '06 #1
Share this question for a faster answer!
Share on Google+

This discussion thread is closed

Replies have been disabled for this discussion.