Jump to content

Scandir AND parse xml file?


Guldstrand

Recommended Posts

Use glob

 

foreach (glob('*.xml')) as $file) {
    $fileData = file_get_contents($file);
     // parsing data here
}

Thanks for your fast reply.

Can you please explain how to include "scandir"?

 

I have this:

$dir    = 'templates';
$files = scandir($dir); 

foreach (glob("*.xml") as $filename) {
    echo "<p>$filename size " . filesize($filename) . "</p>\n";
}

..but it doesn´t output anything.

The below doesn't use scandir but it does the same thing as scandir but it also filters what is in the folder.

 

$dir = "templates";
foreach (glob($dir . "/*.xml") as $filename) {
    echo "<p>$filename size " . filesize($filename) . "</p>\n";
}

 

That should scan the templates dir for xml files.

 

For a scandir example:

$dir    = '/templates';

$files = scandir($dir);
foreach ($files as $filename) {
     if (stristr($filename, ".xml") !== false) {
         $fileData = file_get_contents($file);
         // parsing data here
     }
}

 

As you can see, glob is easier as it pulls what you want, not everything including the kitchen sink.

 

EDIT:

Modified the scandir version to be correct.

Nevermind...  I finally got it. :P

 

I changed it to this:

<?php 
$dir    = 'templates/*'; 

foreach (glob($dir . "/template.xml") as $filename) {
    echo "$filename size " . filesize($filename) . "<br />\n"; 

ini_set('zend.ze1_compatibility_mode', '0'); 
$xml = simplexml_load_file($filename); 

print("<p>
<img src=\"$xml->thumb\" alt=\" $xml->tempname \" title=\" $xml->tempname \" /><br />
Name: $xml->tempname<br />
Created by: <a href=\"mailto:$xml->email\">$xml->creator</a>
</p>"); 
} 
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.