Page 1 of 1

Problem with XML Generated by PHP

Posted: Fri Mar 06, 2009 6:35 pm
by Master_Phantom
Hi,

I have a little problem with a XML file generated with PHP.

I´m making a Flash Gallery that loads the path of the images and thumbnails.

And I have the Admin side that makes an interesting function: It uploads a photo and makes the thumbnail and resizes the original. 8)

The Flash gallery loads a php file that output something like this:

Code: Select all

<?xml version="1.0" encoding='UTF-8'?>
<gallery>
<image>.</image>
<image>..</image>
<image>arches-11.jpg</image>
<image>archipelago-5.jpg</image>
<image>ardara.jpg</image>
<image>brook.jpg</image>
<image>climbing-moss.jpg</image>
<image>los-angeles-skyline.jpg</image>
<image>mountain-109.jpg</image>
<image>thumbs</image>
<image>yosemite-stream.jpg</image>
</gallery>
 
Well this is perfect, because Flash loads all the images with their respective thumbnails, the thing I want to do is to remove the first two image nodes, and the "thumbs" node. Because it confuses the gallery and make it load "blank images". :(

So I ask a way to remove the nodes before they are generated, I use this code:

Code: Select all

<?php
 
$dir = opendir("images");
 
echo "<?xml version=\"1.0\" encoding='UTF-8'?>\n";
echo "<gallery>\n";
 
while (($file = readdir($dir)) !== false)
    {
    echo "<image>" . $file . "</image>\n";
    }
  
echo "</gallery>\n";
  
closedir($dir);
 
?>
Thank you very much 8)

Re: Problem with XML Generated by PHP

Posted: Fri Mar 06, 2009 8:45 pm
by Benjamin

Code: Select all

 
<?php
 
$dir = opendir("images");
 
echo "<?xml version=\"1.0\" encoding='UTF-8'?>\n";
echo "<gallery>\n";
 
while (($file = readdir($dir)) !== false) {
    if (preg_match('#^\.{1,2}$#', $file)) continue;
    echo "<image>" . $file . "</image>\n";
}
  
echo "</gallery>\n";
  
closedir($dir);
 
?>
 

Re: Problem with XML Generated by PHP

Posted: Sat Mar 07, 2009 3:20 am
by BomBas
Astion code will get rid of '..' and '.' - this one gonna get rid of 'thumbs' either.

Code: Select all

 
<?php
 
$dir = opendir("images");
 
echo "<?xml version=\"1.0\" encoding='UTF-8'?>\n";
echo "<gallery>\n";
 
while (($file = readdir($dir)) !== false) {
    if ( preg_match('#^\.{1,2}$#', $file) OR $file == 'thumbs' ) continue;
    echo "<image>" . $file . "</image>\n";
}
  
echo "</gallery>\n";
  
closedir($dir);
 
?>
 

Re: Problem with XML Generated by PHP

Posted: Sat Mar 07, 2009 3:52 am
by requinix
You guys are really coming up with regular expressions to do this? Seriously?

How about you just check that the $file is a file?

Re: Problem with XML Generated by PHP

Posted: Sat Mar 07, 2009 5:53 am
by Benjamin
BomBas wrote:Astion code will get rid of '..' and '.' - this one gonna get rid of 'thumbs' either.

Code: Select all

 
    if ( preg_match('#^\.{1,2}$#', $file) OR $file == 'thumbs' ) continue;
 
Actually, it should be:

Code: Select all

 
    if ( preg_match('#^\.{1,2}$#', $file) OR $file == 'thumbs.db' ) continue;
 
Regex is easy. is_file probably isn't nearly as efficient either.

Re: Problem with XML Generated by PHP

Posted: Sat Mar 07, 2009 9:28 am
by Master_Phantom
Thank you very very much.

You saved me again. :D

Now I can finish my masterpiece :mrgreen:

Thank again, see you later!!!

Re: Problem with XML Generated by PHP

Posted: Mon Mar 09, 2009 10:31 am
by pickle
astions wrote:Regex is easy. is_file probably isn't nearly as efficient either.
Without doing any testing, I'd bet that regex is way less efficient than using is_file() or is_dir(). Regex is a relative hog on resources.