I am using PHP and apache for my program. Where index.php
is an input HTML form that accepts CSV file and some text data and does some validations. In case of proper inputs, calls UpdateData.php
, which does some updations with an input file (using DB data), and after updating, the file gets downloaded using headers.
All I want is to display the Thankyou message after a successful download. I tried to use flags and sessions, but after setting headers every set value gets vanished. So any kinda help is appreciated!
Here's my code!
index.php
<?php
function validate ( $csv_index ) {
$error = "";
// code for CSV file validation check
// returns error content if any error
// else returns false
}
$error = "";
$success = "";
if ( !empty ($_POST['submit']) )
{
//POST data fetch
$tmp_file_name = $_FILES['file']['name'];
$csv_index = $_POST['csv_index'];
// call validate
$error = validate ( $csv_index );
if ($error == false) {
include 'updateData.php';
}
}
?>
<!DOCTYPE html>
<body>
<form id="form" action="index.php" method="POST" enctype="multipart/form-data">
INPUT FILE<input type="file" name="file" id="file" required>
CSV ROW No<input type="text" id="csv_index" name="csv_index" required>
<input type="submit" value="SUBMIT" name="submit">
</form>
<p style="text-align:center;"><b><?php if ( $error !== "" ) echo ( "Error occured"); ?></b></p>
<p id="error" style="color:red;text-align:center;"><b><?php echo ( "$error" ); ?></b></p>
<p style="text-align:center;">
<?php
if ( !empty($_GET["successflag"]) ){
if ($_GET["successflag"] == 1 ) {
echo ( "Thank you! File downloaded");
}}
?>
</p>
</body>
</html>
updateData.php
<?php
//
include_once('dbConfig.php');
// code to update file
// $updatedFileHandler is file pointer/handler of Updated file
// below code simply downloads a file
fseek( $updatedFileHandler, 0 );
header( 'Content-Type: csv; charset=utf-8' );
header( 'Content-Disposition: attachment; filename="' . $NewFileName . '";');
fpassthru( $updatedFileHandler );
fclose( $updatedFileHandler );
?>
After fclose()
I tried to use header("location: index.php?successflag=1");
but it simply stays on same page, displays Success msg without downloading file.
Please suggest if there could be any way to do it!