Reputation: 73
My supplier sends every day an update of the order status with a variable static filename to import in my sql db. My cronjob has the following code:
<?php
require('cron_config.php');
// Open and parse the sales.csv file
$fh = fopen("../files/out/orderstatus.csv", "r");
while ($line = fgetcsv($fh, 1000, ","))
{
$order_id = $line[0];
$order_status_id = $line[1];
$date_modified = $line[2];
// Insert the data into the sales table
$query = "UPDATE oc_order o SET order_status_id='$order_status_id',
date_modified='$date_modified' WHERE order_id = '$order_id'";
$result = $mysqli->query($query);
}
fclose($fh);
$mysqli->close();
?>
In my code you see the static filename "orderstatus.csv". In future I will receive a dynamic filename (example: orderstatus_"order_id".csv. How is it possible to recognize this file with my code? The named order_id is already a part of the sql_query.
Thanks for the help
Upvotes: 1
Views: 917
Reputation: 73
Well done guys. With glob() and unlink() it did the job. Here the code:
<?php
require('cron_config.php');
foreach (glob("../files/out/orderstatus_*.csv") as $filename) {
echo "$filename \n";
}
// Open and parse the sales.csv file
$fh = fopen("$filename", "r");
while ($line = fgetcsv($fh, 1000, ","))
{
$order_id = $line[0];
$order_status_id = $line[1];
$date_modified = $line[2];
// Insert the data into the sales table
$query = "UPDATE oc_order o SET order_status_id='$order_status_id',
date_modified='$date_modified' WHERE order_id = '$order_id'";
$result = $mysqli->query($query);
}
fclose($fh);
unlink("$filename");
$mysqli->close();
?>
Upvotes: 1
Reputation: 3795
Why not search for it:
$result = array_filter(array_map(
function($a){
return strpos($a,'orderstatus_')===0?$a:null;
}
,scandir('../files/out/')));
The result array will only hold all files from the folder that are begin with orderstatus_
.
Upvotes: 0