.xml file not being rewritten.

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
granite
Forum Commoner
Posts: 44
Joined: Mon Feb 09, 2009 10:52 am

.xml file not being rewritten.

Post by granite »

Hello, bros,

I have a php code that should write a .xml file.
Here it goes:

Code: Select all

function xml(){
            
            $xml = "<?xml version=\"1.0\" encoding=\"iso-8859-1\"?>\r\n";
            $xml .= "<noticia>\r\n";
            
            $sel_noticia = mysql_query("SELECT * FROM noticia WHERE status_noticia='a' ORDER BY data_noticia DESC");
            while($row_noticia=mysql_fetch_array($sel_noticia)){
                $xml .= "<item>\r\n";
                $xml .= "<titulo>\r\n";
                $xml .= "<![CDATA[".utf8_encode($row_noticia['titulo_noticia'])."]]>\r\n";
                $xml .= "</titulo>\r\n";
                $xml .= "<texto>\r\n";
                $xml .= "<![CDATA[".utf8_encode($row_noticia['materia_noticia'])."]]>\r\n";
                $xml .= "</texto>\r\n"; 
                $xml .= "<link>\r\n";
                $xml .= "<![CDATA["."ler_noticia.php?id_noticia=".$row_noticia['id_noticia']."]]>\r\n";
                $xml .= "</link>\r\n";
                $xml .= "</item>\r\n";
            }
            $xml .= "</noticia>\r\n";
            
            $open = fopen("../../swf/noticia.xml","w");
            fwrite($open,$xml);
            fclose($open);
So, it should genereate a file called 'noticia.xml', which would be read by a flash. But it doesn't.

Strangely, I renamed this noticia.xml file through a FTP client and the flash cotinues to show the info contained within it. Anyway, why isn't the code above rewriting 'noticia.xml'?
granite
Forum Commoner
Posts: 44
Joined: Mon Feb 09, 2009 10:52 am

Re: .xml file not being rewritten.

Post by granite »

lkjkorn19 wrote:A few basic things:
  • Be sure that you're looking at the correct file, in the correct folder as well. Are you sure it's noticia.xml that is in the folder "../../" (i.e. two folders up) ?
  • Do you suppress PHP errors? Perhaps the file has an invalid CHMOD value.
  • I assume you checked the file's contents manually and not just looked at the Flash file? Since maybe cache would be an influence here, but I doubt that it's the case.
  • This code is in a function. Are you sure that this function is being run?
  • Checked. Correct file. Correct folder.
  • Hm... How and why am I supposed to do that? Also, exactly which file/folder should I check the CHMOD?
  • Yes, I did.
  • No, the function is being run. I added an alert just to be sure.
granite
Forum Commoner
Posts: 44
Joined: Mon Feb 09, 2009 10:52 am

Re: .xml file not being rewritten.

Post by granite »

Ok, thanks. I did what you said, but still...

Below's the modified code. I added your "if" plus one mine. The fwrite fails to write the file. The question is: why? I have already changed the CHMOD to 755 and 777 but the problem persists. Any idea? Oh, btw, your "if" echos "File is not writable. Please use a higher CHMOD value!".

Code: Select all

function xml(){
            $xml = "";
            $xml = "<?xml version=\"1.0\" encoding=\"iso-8859-1\"?>\r\n";
            $xml .= "<noticia>\r\n";
            
            $sel_noticia = mysql_query("SELECT * FROM noticia WHERE status_noticia='a' ORDER BY data_noticia DESC");
            while($row_noticia=mysql_fetch_array($sel_noticia)){
                $xml .= "<item>\r\n";
                $xml .= "<titulo>\r\n";
                $xml .= "<![CDATA[".utf8_encode($row_noticia['titulo_noticia'])."]]>\r\n";
                $xml .= "</titulo>\r\n";
                $xml .= "<texto>\r\n";
                $xml .= "<![CDATA[".utf8_encode($row_noticia['materia_noticia'])."]]>\r\n";
                $xml .= "</texto>\r\n"; 
                $xml .= "<link>\r\n";
                $xml .= "<![CDATA["."ler_noticia.php?id_noticia=".$row_noticia['id_noticia']."]]>\r\n";
                $xml .= "</link>\r\n";
                $xml .= "</item>\r\n";
            }
            $xml .= "</noticia>\r\n";
            
            $open = fopen("../../swf/noticia.xml","w");
            if(is_writable('../../noticias.xml')){
            if(fwrite($open,$xml) != 0){
            ?>
            <script>
            alert("the .xml has supposedly been written");
            </script>
            <?
            }else{
            ?>
            <script>
            alert("a error ocurred while trying to white the .xml");
            </script>
            <?
            }
            echo 'Hurray! XML file is writable.'; 
            }
            else{ 
            echo 'File is not writable. Please use a higher CHMOD value!'; 
            }
            
            fclose($open);
            
            ($sel_noticia);
        }
granite
Forum Commoner
Posts: 44
Joined: Mon Feb 09, 2009 10:52 am

Re: .xml file not being rewritten.

Post by granite »

Yes, I can access noticia.xml directly. And no, I'm not using IIS.

I guess I'll have to contact the host.

Thanks, man. :)
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: .xml file not being rewritten.

Post by pickle »

Not really relevant to your question, but I'd recommend SimpleXML if you have it, or DOMXML to build your XML file, rather than explicitly typing the characters out.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
granite
Forum Commoner
Posts: 44
Joined: Mon Feb 09, 2009 10:52 am

Re: .xml file not being rewritten.

Post by granite »

Oh yeah, I see. Very helpful, since I'm yet a newbie at these xml things!!

Thank you very much! :o
Post Reply