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

Time:05-04

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

My XML file is like this:

<?xml version='1.0' encoding='UTF-8'?>
<Order>
  <OrderType>TBA</OrderType>
  <OrderDate>2022-01-29T08:25:01</OrderDate>
  <SequenceNumber>LB5HVR2JWTTFWR82</SequenceNumber>
  <OrderNumber>STGFR000000048</OrderNumber>
  <Currency>EUR</Currency>e>
  <BillingPhone>0123456789</BillingPhone>
  <OrderLines>
    <Id>1</Id>
    <SKU>SKU</SKU>
    <Code>TCTG4500097</Code>
    <Quantity>1</Quantity>
    <GrossPrice>66.66</GrossPrice>
    <OrderLineSaleTotal>79.99</OrderLineSaleTotal>
  </OrderLines>
  <OrderLines>
    <Id>2</Id>
    <SKU>SKU</SKU>
    <Code>TCTC81009011</Code>
    <Quantity>1</Quantity>
    <GrossPrice>16.62</GrossPrice>
    <OrderLineSaleTotal>19.95</OrderLineSaleTotal>
  </OrderLines>
  <OrderLines>
    <Id>3</Id>
    <SKU>SKU</SKU>
    <Code>TCTC8100497</Code>
    <Quantity>1</Quantity>
    <GrossPrice>16.63</GrossPrice>
    <OrderLineSaleTotal>19.95</OrderLineSaleTotal>
  </OrderLines>
</Order>

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 = "[email protected]";
                    $subject = "Error GEAN";
                    
                    $message = "GEAN not found for $SKU_CODE in file $source_file";
                    
                    $header = "From:[email protected] \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);
?>

CodePudding user response:

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 = "[email protected]";
                $subject = "Error GEAN";

                $message = "GEAN not found for $SKU_CODE in file $source_file";

                $header = "From:[email protected] \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

$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 (is_array($result) && in_array($SKU_CODE, $result)) { // if sku exits in result array, this means that its GEAN is null
                $save = false; // set flag to false in case of error to avoid saving

                // echo "GEAN not found for $SKU_CODE";

                //email part
                $to = "[email protected]";
                $subject = "Error GEAN";

                $message = "GEAN not found for $SKU_CODE in file $source_file";

                $header = "From:[email protected] \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);
?>
  • Related