Results 1 to 2 of 2

Thread: Create .xml file in php

  1. #1
    Join Date
    May 2011
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Create .xml file in php

    I want to save xml data in a separate file of .xml using cake php. i used the following code:

    $a="test";
    $b="test1";
    header('Content-type: text/xml');
    header('Pragma: public');
    header('Cache-control: private');
    header('Expires: -1');
    $xml = "<?xml version=\"1.0\" encoding=\"utf-8\"?>";
    $xml.= "<registrations>";
    $xml.= " <registration>";
    $xml.= " <id>".$a."</id>";
    $xml.= " <name>".$b."</name>";
    $xml.= " </registration>";

    $xml.= "</registrations>";
    $fh = fopen('my_xmls.xml', 'w+');
    fwrite($fh, $xml);
    fclose($fh);

    What is the problem in my code or please give me a new code i'm using cake php.

  2. #2
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    First, why are you sending headers if you are saving a file? Headers are sent directly to the browser to tell the browser what kind of data the server is sending-- you must pick one or the other: save to file or display in the browser. With XML, this means saving a .xml file on the server, or displaying/downloading a .xml document in the browser. If you do decide to save the file on the server, then you can do something else as output for the browser-- for example, you could say echo 'XML Saved!'; or you could even output the .xml directly: echo $xml; in this case in addition to saving it as a file and in that case you would need the headers. Does that make sense?

    Second, your code looks fine for saving the file. Since you have the headers set to be sending the browser xml data, it may not be displaying any errors properly, so remove the headers and see what the output is. Also, turn on error reporting (including warnings, etc.). My best guess is that you don't have the correct permissions set to create that file. Everything else looks fine.



    Also, I don't know much about cake php. Are you sure it allows all of this? Why not just use normal php?
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •