i have PHP code that creates a text file. after the file has been created i want it to be opened in another tab. how do i modify the code below.

<?php
    session_start();
    require_once 'includes/config.php';
    require_once 'includes/functions.php';
    set_time_limit(0); // override the maximum execution time in php.ini file

    if ( isset( $_POST['btnProcess'] ) ) {
        $errors = array();

        if ( $_POST['cboBankID'] == "" ) {
            $errors[] = 'Please select the bank.';
        } else {
            $bankID = ( int )$_POST['cboBankID'];
        }

        if ( $_POST['txtProcessDate'] == "" ) {
            $errors[] = 'Please select the process date.';
        } else {
            $processDate = $_POST['txtProcessDate']; // assign $_POST['txtProcessDate'] to $processDate
            $currentDate = date( 'Y-m-d' ); // assign the current date to $currentDate
            if ( dayDifference( $currentDate, $processDate ) < 0 ) { // process date is earlier than current date
                $errors[] = 'Process date must not be earlier than current date.';
            } elseif( dayDifference( $currentDate, $processDate ) >= 0 ) { // process date is afterthan current date
                $dateArray = explode( "-", $processDate ); // split $processDate using hyphens
                $processDay = $dateArray[2]; // assign the third element of $dateArray to $processDay
                $processDate = str_replace( "-", "", $processDate ); // remove hyphens from $processDate
            }
        }

        if ( $_POST['cboCompanyID'] == "" ) {
            $errors[] = 'Please select the company.';
        } else {
            $companyID = ( int )$_POST['cboCompanyID'];
        }

        if ( empty( $errors ) ) {
            $sql = "SELECT * FROM bankaccounts WHERE companyID = {$companyID} AND bankID = {$bankID}";
            $result = mysql_query( $sql ) or die( mysql_error() );
            $bankRow = mysql_fetch_array( $result );
            $debitOrderID = getPaymentModeID( 'Direct Debit' );
            $lapseID = getPolicyStatusID( 'Lapse' );
            $activeID = getPolicyStatusID( 'Active' );
            $acceptedID = getPolicyStatusID( 'Accepted' );
            $query = "SELECT * FROM policydetails WHERE paymentModeID = {$debitOrderID} AND bankID = {$bankID} AND ";
            $query .= "directDebitDay = {$processDay} AND policyStatusID IN ({$lapseID}, {$activeID}, {$acceptedID} )";
            $queryResult = mysql_query( $query ) or die( "$query<br />" . mysql_error() );
            if ( mysql_num_rows( $queryResult ) > 0 ){ // there are records to process
                $file = 'bankfiles/' . $bankRow['bankCode'] . strtotime( date( 'YmdHis' ) ) . ".txt"; // create the file
                $handle = fopen( $file, 'w' ); // open the file for writing
                // echo 'passes ' . $bankRow['bankID'];
                switch ( $bankRow['sfiVersion'] ) { // use a switch case to take action depending on the value of $bankRow['sfiVersion']
                    case 003 : // $bankRow['sfiVersion'] is equal to 3
                        // echo 'SFI Version is equal to ' . $bankRow['sfiVersion'];
                        $header = "UHL" . $processDate;
                        $senderID = $bankRow['senderID'];
                        $receiverID = $bankRow['receiverID'];
                        if ( strlen( $senderID ) < 9 ) {
                            $senderID = str_pad( $senderID, 9, " ", STR_PAD_RIGHT ); // pad white space to the right of $senderID
                        }
                        $header .= $senderID;
                        if ( strlen( $receiverID ) < 9 ) {
                            $receiverID = str_pad( $receiverID, 9, " ", STR_PAD_RIGHT ); // pad white space to the right of $receiverID
                        }
                        $header .= $receiverID;
                        $fileID = getFileNumber( $bankID );
                        if ( strlen( $fileID ) < 6 ) {
                            $fileID = str_pad( $fileID, 6, "0", STR_PAD_LEFT ); // add leading zeros to $fileID
                        }
                        $header .= $fileID;
                        if ( strlen( $bankRow['workCode'] ) < 15 ) {
                            $workCode = str_pad( $bankRow['workCode'], 15, " ", STR_PAD_RIGHT ); 
                        }
                        $header .= $workCode;
                        $header .= $bankRow['sfiVersion'] . "\n";
                        fwrite( $handle, $header ); // write the header to the file
                        $numRecords = 0; // initizlize $numRecords to 0
                        $debitValue = 0; // initialize $debitValue to 0
                        $creditValue = 0; // initialize $creditValue to 0
                        $creditCount = 0; // initialize $creditCount to 0
                        while( $row = mysql_fetch_array( $queryResult ) ) { // loop the affected policies 
                            $lineRow = "DEB";
                            $branchCode = getBranchCode( $row['bankBranchID'] );
                            $destSort = str_pad( $branchCode, 7, "0", STR_PAD_LEFT );
                            $lineRow .= $destSort;
                            $destAccount = str_pad( $row['bankAccountNumber'], 15, "0", STR_PAD_LEFT );
                            $lineRow .= $destAccount;
                            $lineRow .= "  02";
                            $origSort = str_pad( $bankRow['branchCode'], 7, "0", STR_PAD_LEFT );
                            $lineRow .= $origSort;
                            $origAccount = str_pad( $bankRow['accountNumber'], 15, "0", STR_PAD_LEFT );
                            $lineRow .= $origAccount;
                            $lineRow .= "  " . str_pad( $row['policyNumber'], 15, " ", STR_PAD_RIGHT );
                            $client = $row['firstName'] . " " . $row['firstName'];
                            $client = str_pad( $client, 30, "" , STR_PAD_RIGHT );
                            $lineRow .= $client;
                            $amount = $row['totalContribution'] * 100;
                            $debitValue = $debitValue + $amount; // add $amount to $debitValue
                            $amount = str_pad( $amount, 16, "0", STR_PAD_LEFT );
                            $lineRow .=  $amount . $processDate . "\n";
                            $numRecords++; // increase the value of $numRecords by 1

                            fwrite( $handle, $lineRow ); // write the $lineRow to the file
                        }
                        $trailer = "UTL";
                        $debitValue = str_pad( $debitValue, 16, "0", STR_PAD_LEFT );
                        $creditValue = str_pad( $creditValue, 16, "0", STR_PAD_LEFT );
                        $trailer .= $debitValue . $creditValue . str_pad( $numberRecords, 6, "0", STR_PAD_LEFT );
                        $trailer .= str_pad( $creditCount, 6, "0", STR_PAD_LEFT );
                        fwrite( $handle, $trailer );
                        fclose( $handle ); // close the file
                        updateFileNumber( $bankID );
                        header( "Location: $file" );
                        exit;
                        break;
                    case 004 : // $bankRow['sfiVersion'] is equal to 4
                        // echo 'SFI Version is equal to ' . $bankRow['sfiVersion'];
                        $senderID = $bankRow['senderID'];
                        $receiverID = $bankRow['receiverID'];
                        $workCode = $bankRow['workCode'];
                        $fileID = getFileNumber( $bankID );
                        $fileID = str_pad( $fileID, 6, "0", STR_PAD_LEFT );
                        $sender = str_pad( $senderID, 9, " ", STR_PAD_RIGHT );
                        $receiver = str_pad( $receiverID, 9, " ", STR_PAD_RIGHT );
                        $wCode = str_pad( $workCode, 15, " ", STR_PAD_RIGHT );
                        $header = "UHL" . $processDate . $sender . $receiver . $fileID . $wCode . $bankRow['sfiVersion'] . "\n";
                        fwrite( $handle, $header );
                        while ( $row = mysql_fetch_array( $queryResult ) ) {
                            $detailRecords = "DEB";
                            $branchCode = getBranchCode( $row['bankBranchID'] );
                            $destSort = str_pad( $branchCode, 7, "0", STR_PAD_LEFT );
                            $detailRecords .= $destSort;
                            $destAccount = str_pad( $row['bankAccountNumber'], 20, "0", STR_PAD_LEFT );
                            $detailRecords .= $destAccount . "01USD                        02";
                            $branchCode = str_pad( $bankRow['branchCode'], 7, "0", STR_PAD_LEFT );
                            $detailRecords .= $branchCode;
                            $accountNumber = str_pad( $bankRow['accountNumber'], 20, "0", STR_PAD_LEFT );
                            $detailRecords .= $accountNumber . "  USD                        ";
                            $policyNumber = str_pad( $row['policyNumber'], 15, " ", STR_PAD_RIGHT );
                            $client = $row['firstName'] . ' ' . $row['lastName'];
                            $client = $row['firstName'] . ' ' . $row['lastName'];
                            $client = str_pad( $client, 30, " ", STR_PAD_RIGHT );
                            $detailRecords .= $policyNumber . $client;
                            $amount = $row['totalContribution'] * 100;
                            $debitValue = $debitValue + $amount;
                            $debitCount++; // increase $debitCount by 1
                            $amount = str_pad( $amount, 24, "0", STR_PAD_LEFT );
                            $detailRecords .= $amount . "USD   " . $processDate . "  MOONLIGHT STOP ORDER" . "\n";
                            fwrite( $handle, $detailRecords );
                        }
                        $trailer = "UTLUSD";
                        $debitValue = str_pad( $debitValue, 24, "0", STR_PAD_LEFT );
                        $creditValue = str_pad( $creditValue, 24, "0", STR_PAD_LEFT );
                        $debitCount = str_pad( $debitCount, 6, "0", STR_PAD_LEFT );
                        $creditCount = str_pad( $creditCount, 6, "0", STR_PAD_LEFT );
                        $trailer .= $debitValue . $creditValue . $debitCount . $creditCount;
                        fwrite( $handle, $trailer );
                        fclose( $handle );
                        updateFileNumber( $bankID );                            
                        //download.php
                        //content type
                        header('Content-type: text/plain');
                        //open/save dialog box
                        header( "Content-Disposition: attachment; filename=$file" );
                        //read from server and write to buffer
                        readfile( $file );
                        header( "Location: $file" );
                        exit;
                        break;
                } // end of switch case 
            } else {
                $errors[] = 'There are no records associated with that bank.';
            }
        }
    }

?>

Have you tried opening a new window with Javascript? You can use the window.open() function for that, if I'm not mistaken.

Like @minitauros said, change the header("Location: $file \r\n"); line with something like: echo '<script type="text/javascript">window.open(' . $file . ')</script>';

See if that works.

Member Avatar for diafol

I suppose it could be done with a target="_blank":

<!DOCTYPE html>
<html>
<head>
</head>
<body>

<form method="post" target="_blank" action="newfile.php">
    <input name="text" />
    <input type="submit" name="submit" value="GO NEW" />
</form>

</div>      
</div>
</body>
</html>

Test the newfile.php file with this:

<?php
    print_r($_POST);
?>

Not sure if that's what you want. No need to involve js - just straight html.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.