A couple things. First please put code into code tags using the forum editor. I have fixed your initial post.
Second, you say that you get a "blank screen", but you have provided no content of what debugging you have performed. You state you are looking for another way to iterate over the files - but you have already confirmed that the files do load and you can iterate over them - you just aren't getting results. So, rather than finding a different way to get the files (which you have already accomplished), you need to figure out why it is not working.
I suspect the issue may be your variable names are causing confusion and you aren't passing what you think you are to the processing code. In example you post with a single file you have this:
$file = file('myfile.xml');
In that case $file is an array of the contents of the file. Then you state that you tried thing such as
$file = glob("directory/*");
foreach ($file as $line) {
or
$directory = 'directory';
$files = scandir($directory);
foreach ($files as $file) {
In the first case $line is a $file (not a line) and in the second case $file (in the foreach loop) is a reference to the file NOT an array of the contents of the file. I'm assuming you are not calling file() on the file reference within your loop. If you had called that first variable $linesAry, which is more accurate to what it is, I think you would have seen this yourself.
This should work. Note I don't have a an environment to test on at the moment. So there could be some minor typos\errors.
<?php
//Make the DB connection
$servername = "localhost";
$username = "dbuser";
$password = "dbpass";
$db = "dbdb";
$conn = new mysqli($servername, $username, $password, $db);
if ($conn->connect_error){
die("Connection failed: ". $conn->connect_error);
}
//Get the contents of the directory
$directoryStr = "directory/*";
echo "Processing directory: {$directoryStr}<br>\n";
$filesAry = glob($directoryStr);
echo "Found: " . count($filesAry) . " files<br><br>\n";
//Iterate over each file
foreach ($filesAry as $fileStr)
{
//Reset the variables for the data being searched
$ip = '';
$hostname = '';
$port = '';
$portArray = array();
$portList = '';
$timestamp = '';
//Get content of the file into an array
echo "Processing file: {$fileStr}<br>\n";
$linesAry = file($fileStr);
//Iterate over each line of text in the file
foreach($linesAry as $lineStr)
{
//Get IP Address
if (strpos($lineStr, 'addrtype="ipv4"') == TRUE)
{
preg_match('/addr=".* addrtype/', $lineStr, $results);
$ip = implode(" ",$results);
$ip = ltrim($ip, 'addr="');
$ip = rtrim($ip, '" addrtype');
echo "<br><strong><u>Device</u></strong><br>";
echo "IP Address: $ip<br>";
}
//Get Hostname
if (strpos($lineStr, 'type="PTR"') == TRUE)
{
preg_match('/name=".*" type/',$lineStr,$results);
$hostname = implode(" ",$results);
$hostname = ltrim($hostname,'name="');
$hostname = rtrim($hostname, ' type');
$hostname = rtrim($hostname, '"');
echo "Hostname: $hostname<br>";
}
//Get Ports
if (strpos($lineStr, 'portid="') == TRUE)
{
preg_match('/portid=".*><state/',$lineStr,$results);
$port = implode(" ",$results);
$port = ltrim($port,'portid="');
$port = rtrim($port, '"><state');
echo "Port: $port<br>";
array_push($portArray, $port);
}
//Add Values to Database
if (strpos($lineStr, '/host>') == TRUE)
{
$timestamp = time();
$mytime = new \DateTimeImmutable('@'.$timestamp);
$portList = implode(", ",$portArray);
$sql = "insert into _____ (ip,hostname,ports,timestamp) values ('$ip', '$hostname', '$portList', '$timestamp')";
if ($conn->query($sql) === TRUE) {
echo "Data Added: $ip - $hostname - $portList - $timestamp <br>";
} else {
echo "Error: ".$sql."<br>".$conn->error;
}
}
}
}
//Close the DB connection
$conn->close();
?>