Aloha everyone,
I have another question. I have a SYDI script that retrieves WMI data from Windows computers, then appends a timestamp to the end of the file, and finally saves it to a share on a server. The PHP code I have to read and print out the filenames is shown below:
if (is_dir($dir)) {
if ($dh = opendir($dir)) {
while (($file = readdir($dh)) !== false) {
if ($file != "." && $file != "..") {
$results[] = $file;
echo $file . "<br />";
}
}
closedir($dh);
}
}
Everything is good and the filenames are returned as shown below:
sydiResult-24-Apr-11-18-59-52.xml
sydiResult-24-Apr-11-19-00-32.xml
sydiResult-24-Apr-11-19-01-17.xml
I've tried to use simplexml to call the files, in order, to see if I could even do it, with the following code:
for ($i = 0; $i < sizeof($results); $i++) {
$xml = simplexml_load_file($results[$i]);
}
but I get errors indicating that simplexml couldn't open external xml files. The php file is in my htdocs directory, and the xml files are in a directory in the htdocs directory, called sydiResults. I wanted to separate the sydi xml files, so I decided to make the sub-directory. Could that be the reason simple xml won't work?
My plan is simple: open an the xml files and extract certain information from it, then loop to the next xml file, extract certain information from it, then ... so on and so on until I reach the last xml file in the directory. Does anyone have an idea of how I can use simplexml, or if I can use simplexml to open each xml file in turn?
Thanks in advance for any and all responses.
try this :
for ($i = 0; $i < sizeof($results); $i++) {
$file = 'sydiResults/'.$results[$i];
if (file_exists($file)) {
$xml = simplexml_load_file($file);
print_r($xml);
}
else {
exit('Failed to open '.$file);
}
As I put in the comment above, everything is resolved now! I tested my theory by reading a portion of the data within one of the tags. Here is the code that works:
// Open a known directory, and proceed to read its contents
if (is_dir($dir)) {
if ($dh = opendir($dir)) {
while (($file = readdir($dh)) !== false) {
// Check for the items not desired to be read into the $results array, then fill the array
if ($file != "." && $file != ".." && $file != "syditest01.php" && $file != "sydi.bat" && $file != "sydi-server.vbs") {
$results[] = $file;
// Echo the filenames retrieved as they are read
echo $file . "<br />";
}
}
echo "<br /> <br />";
for ($i = 0; $i < sizeof($results); $i++) {
// Verify that only the desired filenames are actually in the array
echo "Found file: $results[$i] <br />";
// Load the files using the simplexml_load_file() method
$xml = simplexml_load_file($results[$i]);
// Read each filename after it gets read by the method above
echo $results[$i] . " should be loaded now. <br />";
// The foreach() loop extracts the data for the InventoryItems table,then the resulting data is
// assigned a variable to use in the INSERT statemsnt for the database.
foreach($xml->machineinfo[0]->attributes() as $a => $b)
{
// Fill the $info array with the attributes of the machineinfo xml tag
$info[] = $b;
}
// Echo the attributes in the desired order for the INSERT statement for entry into the database
echo $info[2], "<br />";
echo $info[0], "<br />";
echo $info[1], "<br />";
echo $info[3], "<br /><br />";
}
closedir($dh);
}
}
This is the result:
sydiResult-24-Apr-11-18-59-52.xml
sydiResult-24-Apr-11-19-00-32.xml
sydiResult-24-Apr-11-19-01-17.xml
sydiResult-25-Apr-11-13-08-52.xml
sydiResult-25-Apr-11-13-10-47.xml
sydiResult-25-Apr-11-13-12-01.xml
Found file: sydiResult-24-Apr-11-18-59-52.xml
sydiResult-24-Apr-11-18-59-52.xml should be loaded now.
VMware-56 4d 96 72 5e ec d8 5d-e0 f3 b2 ba 2a 55 72 5b
VMware, Inc.
VMware Virtual Platform
Other
Found file: sydiResult-24-Apr-11-19-00-32.xml
sydiResult-24-Apr-11-19-00-32.xml should be loaded now.
VMware-56 4d 96 72 5e ec d8 5d-e0 f3 b2 ba 2a 55 72 5b
VMware, Inc.
VMware Virtual Platform
Other
Found file: sydiResult-24-Apr-11-19-01-17.xml
sydiResult-24-Apr-11-19-01-17.xml should be loaded now.
VMware-56 4d 96 72 5e ec d8 5d-e0 f3 b2 ba 2a 55 72 5b
VMware, Inc.
VMware Virtual Platform
Other
Found file: sydiResult-25-Apr-11-13-08-52.xml
sydiResult-25-Apr-11-13-08-52.xml should be loaded now.
VMware-56 4d 96 72 5e ec d8 5d-e0 f3 b2 ba 2a 55 72 5b
VMware, Inc.
VMware Virtual Platform
Other
I'm running this on my Windows 7 virtual machine on my MacBook Pro, so the only differing information is the filename, but this at least proves that it is reading each file in turn, which is exactly the behavior I want to see!