Jump to content

[SOLVED] PHP readdir to XML output issues


dimlylitstar

Recommended Posts

Hey! I'm having some troubles. I am new to PHP and very slowly trying to figure it out. Here's what I am trying to do:

 

I'm trying to read a directory called Photography, gather all of the file names and then output them in XML that I will then load into flash. Right now I'm just trying to test the PHP code to make sure it outputs correctly and, surprise, it's not working. 

 

I'm testing it by making it the index page of the web server I'm working from so I suppose it's possible that I'm doing it wrong in the first place (i'm pretty new to PHP). So let me know if you can see anything wrong with the code or if I'm testing it wrong.

 

Thanks in advance,

 

Here's the PHP:

<html>
<body>
<?php
$dir = '../Photography/';
$file_ext = ".jpg";
        header ("content-type: text/xml");  
        $xml = <?xml version="1.0\" encoding=\"UTF-8\"standalone=\"yes\"?>\n';
        $xml .= "<content>\n";
	$xml .= "<gallery>\n";
      //Open directory, read contents and add to file_list if correct file_type
      if (is_dir($dir)) {
      	if ($dh = opendir($dir)) {
            	while (($file = readdir($dh)) !== false) {
                		if ($file != '.' && $file != '..') {
                   		$name_array = explode('_', $file);
                   			if ($name_array[1] == $file_ext) {		
                			      $file_img = "$file";
                  		 	$xml .= "<image>" . $file_img . $file_ext "</image>\n";
                   			}
                			}
             		}
             	closedir($dh);
            	$xml .= "</gallery>\n";
           		$xml .= "</content>\n";
     		$wdir = ''.$dir.'images.xml';
     		$f=fopen("$wdir","w") or die("Could not open and empty the file");
           		fwrite($f,$xml) or die("Could not write to the file");
           		fclose($f) or die ("Could not close the resource");
    	      }
       }
'<?xml version=\"1.0\" encoding=\"UTF-8\"standalone=\"yes\"?>\n';
echo $xml;
?>
</body>
</html>

 

 

And here's the output it's giving in the browser:

\n'; $xml .= "\n"; $xml .= "\n"; //Open directory, read contents and add to file_list if correct file_type if (is_dir($dir)) { if ($dh = opendir($dir)) { while (($file = readdir($dh)) !== false) { if ($file != '.' && $file != '..') { $name_array = explode('_', $file); if ($name_array[1] == $file_ext) { $file_img = "$file"; $xml .= "" . $file_img . $file_ext "\n"; } } } closedir($dh); $xml .= "\n"; $xml .= "\n"; $wdir = ''.$dir.'images.xml'; $f=fopen("$wdir","w") or die("Could not open and empty the file"); fwrite($f,$xml) or die("Could not write to the file"); fclose($f) or die ("Could not close the resource"); } } '\n'; echo $xml; ?>

 

So I think it's pretty clear that my code needs some love.

 

Thanks for your help.

 

 

Link to comment
Share on other sites

Here is how I would do it:

<?php
  $dir = '../Photography/';
  $file_ext = ".jpg";
  
  $xml = new SimpleXMLElement('<content />');
  $gallery = $xml->addChild('gallery');
  foreach(glob($dir.'*'.$file_ext) as $file){
    $gallery->addChild('image',$file);
  }
  file_put_contents($dir.'images.xml',$xml->asXML());

  header("Content-type: text/xml;");
  echo $xml->asXML();
?>

Link to comment
Share on other sites

rhodesa, Thanks for the reply!

 

I tried your code. Basically, I'm just trying to test the PHP to see if the XML even comes out. So I'm just pasting the code in my index.html file. Is this even the right way to test PHP? I've been having major issues with my swf so I wanted to back up and see if the PHP was part of the problem.

 

So I tried your code and the output in the browser is:

 

'); $gallery = $xml->addChild('gallery'); foreach(glob($dir.'*'.$file_ext) as $file){ $gallery->addChild('image',$file); } file_put_contents($dir.'images.xml',$xml->asXML()); header("Content-type: text/xml;"); echo $xml->asXML(); ?> 

 

It looks like as soon as the xml is declared, it's being treated like a string? I'm not sure what the deal is. Do you have any ideas? Sorry to be so much trouble.

Link to comment
Share on other sites

I tested the code you provided with the swf and the flash player gave me the following error message:

TypeError: Error #1085: The element type "br" must be terminated by the matching end-tag "</br>".

 

This is located in the chunk of code that loads the xml created by the php file. I don't know if this is coming from the as3 code or something in the php code, but I thought I'd let you know about that in case that helps point to the problem. There is no element type "br" that I could find anywhere in any of the code. I know <br> is a line break in html, but I don't know how that applies to this particular issue. I guess that might point to it being an as3 error in the resulting html code when the swf is published?

 

Anyway, extra info in case it helps. I'll keep trying to figure this out and please, if anyone has any ideas, let me know.

 

Thanks.

 

 

Link to comment
Share on other sites

Okay so I got it to work but it's throwing errors.

 

Here's the information in the browser:

"This page contains the following errors:

 

error on line 5 at column 527: XML declaration allowed only at the start of the document

Below is a rendering of the page up to the first error.

 

PHP Error Message Warning: Invalid argument supplied for foreach() in /.../.../.../index.php on line 8 Free Web HostingPHP Error Message Warning: file_put_contents(../Photography/images.xml) [function.file-put-contents]: failed to open stream: No such file or directory in /.../.../.../index.php on line 11 Free Web Hosting

(the ... is editing out the exact address).

 

Here's the information from Feed Validator:

line 1, column 0: Undefined root element: br [help]

 

<br><table border='1' cellpadding='2' bgcolor='#FFFFDF' bordercolor='#E8B900 ...

line 5, column 253: XML parsing error: <unknown>:5:253: xml declaration not at start of external entity [help]

 

... sting</font></a></div></td></tr></table><?xml version="1.0"?>

                                            ^

In addition, interoperability with the widest range of feed readers could be improved by implementing the following recommendation.

 

"text/xml" media type is not specific enough [help]

 

 

I'll keep doing my own research, but of course if anyone has any suggestions, please let me know. And thanks for all your help so far.

Link to comment
Share on other sites

ok...try this:

<?php
  $dir = '../Photography/';
  $file_ext = ".jpg";
  
  if(!is_dir($dir))
    die("Dir doesn't exist");
  $files = glob($dir.'*'.$file_ext);
  if(!is_array($files) || !count($files))
    die("No files found in dir");
  
  $xml = new SimpleXMLElement('<content />');
  $gallery = $xml->addChild('gallery');
  foreach($files as $file){
    $gallery->addChild('image',$file);
  }
  file_put_contents($dir.'images.xml',$xml->asXML());

  header("Content-type: text/xml;");
  echo $xml->asXML();
?>

Link to comment
Share on other sites

Hey thanks for all of your help first of all.

 

I tried the new code and it did indicate that the directory didn't exist. So I changed the directory location and it's not giving me that message anymore so that's one problem solved.

 

It's still giving me the other errors though:

This feed does not validate.

 

line 1, column 0: Undefined root element: br [help]

<br><table border='1' cellpadding='2' bgcolor='#FFFFDF' bordercolor='#E8B900 ...

 

line 3, column 253: XML parsing error: <unknown>:3:253: xml declaration not at start of external entity [help]

... sting</font></a></div></td></tr></table><?xml version="1.0"?>

                                                                    ^

In addition, interoperability with the widest range of feed readers could be improved by implementing the following recommendation.

 

 

I did a little research on the XML parsing error and it seems that there must be some output that's being created before the XML declaration. I haven't been able to figure out why that would be. I'll keep looking into it later on tonight and I'll let you know if I figure it out.

If you know of anything that might be causing the error, let me know as well. Once this is solved, I'll try to give a detailed explanation and links and such in case someone else comes along with a similar issue.

Link to comment
Share on other sites

I'm not sure what you mean exactly. I'm not trying to print a table at all. The code that you gave me is the only code. That's it. I'll be pulling in the resulting xml to a swf, but that's all of the php code. I'm not pulling any other code from anywhere. I have no idea why there is a table being printed.

 

Here's the code for the index.php file:

<?php
  $dir = '/home/.../.../Photography/';
  $file_ext = ".jpg";

  $xml = new SimpleXMLElement('<content/>');
  if(!is_dir($dir))
    die("Dir doesn't exist");
  $files = glob($dir.'*'.$file_ext);
  if(!is_array($files) || !count($files))
    die("No files found in dir");

  $gallery = $xml->addChild('gallery');
  foreach($files as $file){
    $gallery->addChild('image',$file);
  }
  file_put_contents($dir.'images.xml',$xml->asXML());
  header("Content-type: text/xml;");
  echo $xml->asXML();
?>

 

That's it. I have no idea where the table is coming from. The directory I'm trying to read is just a folder with image files in it. You ask a legitimate question, because it seems like there's something else going on, but that's all of the code.

 

Here's the entire browser result:

This page contains the following errors:

 

error on line 3 at column 272: XML declaration allowed only at the start of the document

Below is a rendering of the page up to the first error.

 

PHP Error Message Warning: file_put_contents(/home/.../.../Photography/images.xml) [function.file-put-contents]: failed to open stream: Permission denied in /home/.../.../index.php on line 16 Free Web Hosting

 

Here's the entire result for in the Feed Validator:

Sorry

 

This feed does not validate.

 

line 1, column 0: Undefined root element: br [help]

 

<br><table border='1' cellpadding='2' bgcolor='#FFFFDF' bordercolor='#E8B900 ...

line 3, column 253: XML parsing error: <unknown>:3:253: xml declaration not at start of external entity [help]

 

... sting</font></a></div></td></tr></table><?xml version="1.0"?>

                                            ^

In addition, interoperability with the widest range of feed readers could be improved by implementing the following recommendation.

 

"text/xml" media type is not specific enough [help]

 

 

Source: http://.../index.php

 

<br><table border='1' cellpadding='2' bgcolor='#FFFFDF' bordercolor='#E8B900' align='center'><tr><td><font face='Arial' size='1' color='#000000'><b>PHP Error Message</b></font></td></tr></table><br />

<b>Warning</b>:  file_put_contents(/home/.../.../Photography/images.xml) [<a href='function.file-put-contents'>function.file-put-contents</a>]: failed to open stream: Permission denied in <b>/home/.../.../index.php</b> on line <b>16</b><br />

<br><table border='1' cellpadding='2' bgcolor='#FFFFDF' bordercolor='#E8B900' align='center'><tr><td><div align='center'><a href='http://www.000webhost.com/'><font face='Arial' size='1' color='#000000'>Free Web Hosting</font></a></div></td></tr></table><?xml version="1.0"?>

<content><gallery><image>/home/.../.../Photography/Bug.jpg</image><image>/home/.../.../Photography/Cardinal baby2.jpg</image><image>/home/.../.../Photography/Cicada.jpg</image><image>/home/.../.../Photography/Clematis4.jpg</image><image>/home/.../.../Photography/DSCN0561.jpg</image><image>/home/.../.../Photography/Dogwood red berry.jpg</image><image>/.../.../.../Photography/Fern with sun stripe (2).jpg</image><image>/home/.../.../Photography/Ferns in Rows (3).jpg</image><image>/home/.../.../Photography/Frog.jpg</image><image>/home/.../.../Photography/Gladiolus interior 4 (2).jpg</image><image>/home/.../.../Photography/Hosta Flowers 3.jpg</image><image>/home/.../.../Photography/Hummingbird 6 (5).jpg</image><image>/home/.../.../Photography/Hummingbird 7 (2).jpg</image><image>/home/.../.../Photography/Jap Maple with dew.jpg</image><image>/home/.../.../Photography/Peony Blossom2 (3).jpg</image><image>/home/.../.../Photography/Vinca.jpg</image><image>/home/.../.../Photography/bee on cone flower (3).jpg</image><image>/home/.../.../Photography/birch leaves.jpg</image><image>/home/.../.../Photography/bumble best 2 (2).jpg</image><image>/home/.../.../Photography/clematis1 (2).jpg</image><image>/home/.../.../Photography/dogwood blossom2 (2).jpg</image><image>/home/.../.../Photography/evergreen new growth (2).jpg</image><image>/home/.../.../Photography/hosta interior (3).jpg</image><image>/home/.../.../Photography/yellow maple leaf on evergreen (2).jpg</image></gallery></content>

 

<!-- www.000webhost.com Analytics Code -->

<script type="text/javascript" src="http://analytics.hosting24.com/count.php"></script>

<noscript><a href="http://www.hosting24.com/"><img src="http://analytics.hosting24.com/count.php" alt="web hosting" /></a></noscript>

<!-- End Of Analytics Code -->

 

 

It looks like it's reading the directory alright. I might need to do some research to figure out why Permission was denied. Do I need to load a crossdomain file? I have one, but maybe I should have the PHP load it?

 

As far as the other error, I don't know. It must have something to do with the table, but I have no idea where it's getting that from.

 

I'll keep looking around and see if I can find something about it.

Link to comment
Share on other sites

I changed the permissions on my image directory and I got rid of the <br> table error business, but I am having other errors now. But I am actually getting the files outputting in the browser now so that's a step forward.

 

Here are the new error messages:

This feed does not validate.

 

line 2, column 0: Undefined root element: content [help]

 

<content><gallery><image>/home/.../.../Photography/Bug.jpg</ima ...

line 5, column 0: XML parsing error: <unknown>:5:0: junk after document element [help]

 

<script type="text/javascript" src="http://analytics.hosting24.com/count.php ...

In addition, interoperability with the widest range of feed readers could be improved by implementing the following recommendation.

 

"text/xml" media type is not specific enough [help]

 

 

Source: http://.../index.php

 

<?xml version="1.0"?>

<content><gallery><image>/home/.../.../Photography/Bug.jpg</image><image>/home/.../.../Photography/Cardinal baby2.jpg</image><image>/home/.../.../Photography/Cicada.jpg</image><image>/home/.../.../Photography/Clematis4.jpg</image><image>/home/.../.../Photography/DSCN0561.jpg</image><image>/home/.../.../Photography/Dogwood red berry.jpg</image><image>/home/.../.../Photography/Fern with sun stripe (2).jpg</image><image>/home/.../.../Photography/Ferns in Rows (3).jpg</image><image>/home/.../.../Photography/Frog.jpg</image><image>/home/.../.../Photography/Gladiolus interior 4 (2).jpg</image><image>/home/.../.../Photography/Hosta Flowers 3.jpg</image><image>/home/.../.../Photography/Hummingbird 6 (5).jpg</image><image>/home/.../.../Photography/Hummingbird 7 (2).jpg</image><image>/home/.../.../Photography/Jap Maple with dew.jpg</image><image>/home/.../.../Photography/Peony Blossom2 (3).jpg</image><image>/home/.../.../Photography/Vinca.jpg</image><image>/home/.../.../Photography/bee on cone flower (3).jpg</image><image>/home/.../.../Photography/birch leaves.jpg</image><image>/home/.../.../Photography/bumble best 2 (2).jpg</image><image>/home/.../.../Photography/clematis1 (2).jpg</image><image>/home/.../.../Photography/dogwood blossom2 (2).jpg</image><image>/home/.../.../Photography/evergreen new growth (2).jpg</image><image>/home/.../.../Photography/hosta interior (3).jpg</image><image>/home/.../.../Photography/yellow maple leaf on evergreen (2).jpg</image></gallery></content>

<!-- www.000webhost.com Analytics Code -->

<script type="text/javascript" src="http://analytics.hosting24.com/count.php"></script>

<noscript><a href="http://www.hosting24.com/"><img src="http://analytics.hosting24.com/count.php" alt="web hosting" /></a></noscript>

<!-- End Of Analytics Code -->

 

 

 

If anyone knows what these are talking about, let me know. I'll keep looking into it.

 

Thanks.

Link to comment
Share on other sites

The main part of the XML outputted by the PHP is good:

<?xml version="1.0"?>
<content><gallery><image>/home/.../.../Photography/Bug.jpg</image><image>/home/.../.../Photography/Cardinal baby2.jpg</image><image>/home/.../.../Photography/Cicada.jpg</image><image>/home/.../.../Photography/Clematis4.jpg</image><image>/home/.../.../Photography/DSCN0561.jpg</image><image>/home/.../.../Photography/Dogwood red berry.jpg</image><image>/home/.../.../Photography/Fern with sun stripe (2).jpg</image><image>/home/.../.../Photography/Ferns in Rows (3).jpg</image><image>/home/.../.../Photography/Frog.jpg</image><image>/home/.../.../Photography/Gladiolus interior 4 (2).jpg</image><image>/home/.../.../Photography/Hosta Flowers 3.jpg</image><image>/home/.../.../Photography/Hummingbird 6 (5).jpg</image><image>/home/.../.../Photography/Hummingbird 7 (2).jpg</image><image>/home/.../.../Photography/Jap Maple with dew.jpg</image><image>/home/.../.../Photography/Peony Blossom2 (3).jpg</image><image>/home/.../.../Photography/Vinca.jpg</image><image>/home/.../.../Photography/bee on cone flower (3).jpg</image><image>/home/.../.../Photography/birch leaves.jpg</image><image>/home/.../.../Photography/bumble best 2 (2).jpg</image><image>/home/.../.../Photography/clematis1 (2).jpg</image><image>/home/.../.../Photography/dogwood blossom2 (2).jpg</image><image>/home/.../.../Photography/evergreen new growth (2).jpg</image><image>/home/.../.../Photography/hosta interior (3).jpg</image><image>/home/.../.../Photography/yellow maple leaf on evergreen (2).jpg</image></gallery></content>

but this stuff after shouldn't be there:

<!-- www.000webhost.com Analytics Code -->
<script type="text/javascript" src="http://analytics.hosting24.com/count.php"></script>
<noscript><a href="http://www.hosting24.com/"><img src="http://analytics.hosting24.com/count.php" alt="web hosting" /></a></noscript>
<!-- End Of Analytics Code -->

any idea where that is coming from?

 

edit: p.s. - once we get this to valid XML...we'll have to work on getting those paths to the Photography folder correct. Right now they are the filesystem path, but you probably want the url path (easy fix)

Link to comment
Share on other sites

It passed on that validator so it does look like the php is working! Thank you for all of your help. I really appreciate it. This has been a really frustrating experience so far and I still have a ways to go in the as3 part, but now I have a major step working properly. I am definitely going to have to learn more about php. It seems like it's a really good language to know.

 

Thank you very very much. I hope this thread helps someone else.

Link to comment
Share on other sites

Hopefully this will be the last question. I'm very close to getting this to work, but flash isn't liking the full path. It will load an image with just the filename (ex image.jpg) but not with a full path (ex. home/.../.../Photography/image.jpg). So I am trying to get just the filename. I tried using basename in the PHP but it wasn't liking anything I did with it. I tried it in a few different places and it wasn't happy with it.

 

I'm open to doing this is as3 also, but I figured it would probably be easier in the php.

 

Is basename the right way to go? If so, how do I use it in this particular code?

 

Thanks

Link to comment
Share on other sites

Nevermind.

 

I got it!  For anyone who needs it, here's the code:

<?php
  $dir = '/home/.../.../Photography/';
  $file_ext = ".jpg";
  
  if(!is_dir($dir))
    die("Dir doesn't exist");
  $files = glob($dir.'*'.$file_ext);
  if(!is_array($files) || !count($files))
    die("No files found in dir");
  
  $xml = new SimpleXMLElement('<content/>');
  $gallery = $xml->addChild('gallery');
  foreach($files as $file){
    $gallery->addChild('image', basename($file));
  }

  file_put_contents($dir.'images.xml',$xml->asXML());

  header("Content-type: text/xml;");
  echo $xml->asXML();
?>	

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.