Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

<?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);
?>

>Solution :

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);
?>
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading