0

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!

Kelvin R
  • 23
  • 5
  • On updateData.php page, I think it is force download. You can use `readfile()` instead. [See example](https://stackoverflow.com/a/7263943/128761). Make sure that this download works before continue on success message. – vee Nov 27 '21 at 19:26
  • About success message after download. Use `header()` after send out file download is not recommended because it will be error _header already sent_. The download page should donate for download function only. To display success you should use JavaScript to detect link clicked and [delay redirect](https://stackoverflow.com/questions/9877263/time-delayed-redirect/16541769). – vee Nov 27 '21 at 19:31
  • @veeThank you for kind advice! IG Javascript supposes to execute before PHP, plus I have function called `validate()` . In case of error, file shouldn't be downloaded neither success msg will displayed. As per your advise, if I use JS onClick (submit button), it should work after `if ($error == false) ` and before `include 'updateData.php';` in `index.php` . Hence not able to execute JS in between PHP :( – Kelvin R Nov 28 '21 at 06:09

0 Answers0