Page 1 of 1

.xml file not being rewritten.

Posted: Fri Mar 06, 2009 10:23 am
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'?

Re: .xml file not being rewritten.

Posted: Fri Mar 06, 2009 11:56 am
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.

Re: .xml file not being rewritten.

Posted: Fri Mar 06, 2009 12:30 pm
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);
        }

Re: .xml file not being rewritten.

Posted: Fri Mar 06, 2009 1:13 pm
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. :)

Re: .xml file not being rewritten.

Posted: Fri Mar 06, 2009 4:14 pm
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.

Re: .xml file not being rewritten.

Posted: Fri Mar 06, 2009 10:39 pm
by granite
Oh yeah, I see. Very helpful, since I'm yet a newbie at these xml things!!

Thank you very much! :o