Problem with XML Generated by PHP

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Master_Phantom
Forum Newbie
Posts: 8
Joined: Thu Jun 05, 2008 6:49 pm

Problem with XML Generated by PHP

Post 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)
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Re: Problem with XML Generated by PHP

Post 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);
 
?>
 
BomBas
Forum Commoner
Posts: 41
Joined: Wed Mar 04, 2009 1:04 pm

Re: Problem with XML Generated by PHP

Post 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);
 
?>
 
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Problem with XML Generated by PHP

Post 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?
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Re: Problem with XML Generated by PHP

Post 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.
Master_Phantom
Forum Newbie
Posts: 8
Joined: Thu Jun 05, 2008 6:49 pm

Re: Problem with XML Generated by PHP

Post 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!!!
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: Problem with XML Generated by PHP

Post 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.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
Post Reply