How to stop moving a file when an error is found in PHP?

I have a PHP script that allows me to modify the tags of an XML file according to an SQL query and to move this file to a folder once it has been processed.

I added a condition on an error, if the SQL query returns nothing I send an email to be notified. It works well but the file with the error is still moved to the folder. I would like this one to stay in the base folder and the script to continue on the other files.

I tried to change the order of execution of the script several times but without success... can you help me? Thanks

And my script:

<?php
include "include/ODBCaccess.class.php";

$connect = odbc_connect("localhost","root","");
$dirname_source = "D:/xampp/htdocs/xml/";
$dirname_destination = "D:/xampp/htdocs/new_xml/";


$dir = opendir($dirname_source);
while($file = readdir($dir))
{
   $source_file =  $dirname_source.$file;
   $destination_file =  $dirname_destination."Order_".$file;
   if(is_file($source_file))
    {
        // echo $source_file;echo "<br>";
        // echo $destination_file;echo "<br>";
        
        $xml =new DOMDocument("1.0","UTF-8");
        $xml->load($source_file);
        
        $xpath = new DOMXPath($xml);
        foreach ($xpath->query("/Order/OrderLines") as $node)
        {
            $SKU_CODE = $node->getElementsByTagName("Code")->item(0)->nodeValue;
                      
            $query = "select GEAN from SKU where SKU='".$SKU_CODE."'";
            // echo $query; echo "<br>";
            $exec = odbc_exec($connect, $query);
            $result = odbc_fetch_array($exec);

            //ERROR    
            if ($result === false || $result['GEAN'] === null) {
                    // echo "GEAN not found for $SKU_CODE";
                    
                    //email part
                    $to = "test@gmail.com.com";
                    $subject = "Error GEAN";
                    
                    $message = "GEAN not found for $SKU_CODE in file $source_file";
                    
                    $header = "From:noreply@gmail.com \r\n";
                    $header .= "MIME-Version: 1.0\r\n";
                    $header .= "Content-type: text/html\r\n";
                    
                    $retval = mail ($to,$subject,$message,$header);
                    
                    if( $retval == true ) {
                        echo "Message sent successfully...";
                    }else {
                        echo "Message could not be sent...";
                    }
         
                }

                // $barcode = (string) $result['GEAN'];
                // echo $barcode; echo "<br>"; //9353970875729   
                            
                $node->getElementsByTagName("SKU")->item(0)->nodeValue = "";
                $node->getElementsByTagName("SKU")->item(0)->appendChild($xml->createTextNode($result['GEAN']));
                                
        }

        $xml->formatOutput = true;
        $xml->save($source_file);
        rename($source_file,$destination_file);
    }
}
closedir($dir);
?>

1 answer

  • answered 2022-05-04 10:37 Haseeb Hassy

    You can create a flag with default value as true, and in case of an error, set it to false, and only save the file if the flag is true, like shown below:

    <?php
    include "include/ODBCaccess.class.php";
    
    $connect = odbc_connect("localhost", "root", "");
    $dirname_source = "D:/xampp/htdocs/xml/";
    $dirname_destination = "D:/xampp/htdocs/new_xml/";
    
    
    $dir = opendir($dirname_source);
    while ($file = readdir($dir)) {
        $source_file = $dirname_source . $file;
        $destination_file = $dirname_destination . "Order_" . $file;
        if (is_file($source_file)) {
            // echo $source_file;echo "<br>";
            // echo $destination_file;echo "<br>";
    
            $xml = new DOMDocument("1.0", "UTF-8");
            $xml->load($source_file);
    
            $xpath = new DOMXPath($xml);
    
            $save = true; // default flag to allow saving
            foreach ($xpath->query("/Order/OrderLines") as $node) {
                $SKU_CODE = $node->getElementsByTagName("Code")->item(0)->nodeValue;
    
                $query = "select GEAN from SKU where SKU='" . $SKU_CODE . "'";
                // echo $query; echo "<br>";
                $exec = odbc_exec($connect, $query);
                $result = odbc_fetch_array($exec);
    
                //ERROR
                if ($result === false || $result['GEAN'] === null) {
                    $save = false; // set flag to false in case of error to avoid saving
    
                    // echo "GEAN not found for $SKU_CODE";
    
                    //email part
                    $to = "test@gmail.com.com";
                    $subject = "Error GEAN";
    
                    $message = "GEAN not found for $SKU_CODE in file $source_file";
    
                    $header = "From:noreply@gmail.com \r\n";
                    $header .= "MIME-Version: 1.0\r\n";
                    $header .= "Content-type: text/html\r\n";
    
                    $retval = mail($to, $subject, $message, $header);
    
                    if ($retval == true) {
                        echo "Message sent successfully...";
                    } else {
                        echo "Message could not be sent...";
                    }
    
                }
    
                // $barcode = (string) $result['GEAN'];
                // echo $barcode; echo "<br>"; //9353970875729
    
                $node->getElementsByTagName("SKU")->item(0)->nodeValue = "";
                $node->getElementsByTagName("SKU")->item(0)->appendChild($xml->createTextNode($result['GEAN']));
    
            }
    
            if ($save) { // save if the flag is true
                $xml->formatOutput = true;
                $xml->save($source_file);
                rename($source_file, $destination_file);
            }
        }
    }
    closedir($dir);
    ?>
    

    Update:

    get array of all SKUs that have GEAN null, and check in loop if loop SKU exits in array, like shown below:

    <?php
    include "include/ODBCaccess.class.php";
    
    $connect = odbc_connect("localhost", "root", "");
    $dirname_source = "D:/xampp/htdocs/xml/";
    $dirname_destination = "D:/xampp/htdocs/new_xml/";
    
    $query = "select SKU from SKU where GEAN IS NULL";
    
    $exec = odbc_exec($connect, $query);
    $result = odbc_fetch_array($exec); // array of all SKUs where GEAN is null
    
    $exec = odbc_exec($connect, "select SKU from SKU");
    $allSKUs = odbc_fetch_array($exec); // array of all SKUs where GEAN is null
    
    $dir = opendir($dirname_source);
    while ($file = readdir($dir)) {
        $source_file = $dirname_source . $file;
        $destination_file = $dirname_destination . "Order_" . $file;
        if (is_file($source_file)) {
            // echo $source_file;echo "<br>";
            // echo $destination_file;echo "<br>";
    
            $xml = new DOMDocument("1.0", "UTF-8");
            $xml->load($source_file);
    
            $xpath = new DOMXPath($xml);
    
            $save = true; // default flag to allow saving
            foreach ($xpath->query("/Order/OrderLines") as $node) {
                $SKU_CODE = $node->getElementsByTagName("Code")->item(0)->nodeValue;
    
                //ERROR
                if (($result && in_array($SKU_CODE, $result)) || !in_array($SKU_CODE, $allSKUs)) { // if sku exits in result array, this means that its GEAN is null OR SKU is not present in the DB
                    $save = false; // set flag to false in case of error to avoid saving
    
                    // echo "GEAN not found for $SKU_CODE";
    
                    //email part
                    $to = "test@gmail.com.com";
                    $subject = "Error GEAN";
    
                    $message = "GEAN not found for $SKU_CODE in file $source_file";
    
                    $header = "From:noreply@gmail.com \r\n";
                    $header .= "MIME-Version: 1.0\r\n";
                    $header .= "Content-type: text/html\r\n";
    
                    $retval = mail($to, $subject, $message, $header);
    
                    if ($retval == true) {
                        echo "Message sent successfully...";
                    } else {
                        echo "Message could not be sent...";
                    }
    
                }
    
                // $barcode = (string) $result['GEAN'];
                // echo $barcode; echo "<br>"; //9353970875729
    
                $node->getElementsByTagName("SKU")->item(0)->nodeValue = "";
                $node->getElementsByTagName("SKU")->item(0)->appendChild($xml->createTextNode($result['GEAN']));
    
            }
    
            if ($save) { // save if the flag is true
                $xml->formatOutput = true;
                $xml->save($source_file);
                rename($source_file, $destination_file);
            }
        }
    }
    closedir($dir);
    ?>
    

How many English words
do you know?
Test your English vocabulary size, and measure
how many words do you know
Online Test
Powered by Examplum