在 PHP 中发现错误时如何停止移动文件?
How to stop moving a file when an error is found in PHP?
我有一个 PHP 脚本,它允许我根据 SQL 查询修改 XML 文件的标签,并将该文件移动到文件夹中已处理。
我加条件就出错了,如果SQL查询returns什么都没有我发邮件通知。它运行良好,但有错误的文件仍被移动到文件夹中。我希望这个保留在基础文件夹中,脚本继续处理其他文件。
我多次尝试更改脚本的执行顺序但都没有成功...你能帮帮我吗?谢谢
还有我的脚本:
<?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);
?>
您可以创建默认值为 true
的 flag
,如果是 error
,请将其设置为 false
,并且仅在以下情况下保存文件flag
是 true
,如下所示:
<?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);
?>
更新:
获取具有 GEAN null
的所有 SKU 的数组,如果循环 SKU 在 array
中退出,则检查循环,如下所示:
<?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);
?>
我有一个 PHP 脚本,它允许我根据 SQL 查询修改 XML 文件的标签,并将该文件移动到文件夹中已处理。
我加条件就出错了,如果SQL查询returns什么都没有我发邮件通知。它运行良好,但有错误的文件仍被移动到文件夹中。我希望这个保留在基础文件夹中,脚本继续处理其他文件。
我多次尝试更改脚本的执行顺序但都没有成功...你能帮帮我吗?谢谢
还有我的脚本:
<?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);
?>
您可以创建默认值为 true
的 flag
,如果是 error
,请将其设置为 false
,并且仅在以下情况下保存文件flag
是 true
,如下所示:
<?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);
?>
更新:
获取具有 GEAN null
的所有 SKU 的数组,如果循环 SKU 在 array
中退出,则检查循环,如下所示:
<?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);
?>