1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Create a downloadable XML file from PHP output?

Discussion in 'PHP' started by timallard, Aug 16, 2007.

  1. #1
    Hello,

    I have a script i have been working on that does a whole bunch of stuff..
    It echos out XML code, which now you manually have to copy and paste into a wordpad doc then save as XML.

    Is there an easy way to take the output code and write it to an XML file so all the user would have to do is click "download XML doc".

    Thanks!!!
    -Tim
     
    timallard, Aug 16, 2007 IP
  2. jestep

    jestep Prominent Member

    Messages:
    3,659
    Likes Received:
    215
    Best Answers:
    19
    Trophy Points:
    330
    #2
    Your could create the xml and then use a header to force a download.

    Here's a decent script that will help:
    http://www.phpit.net/code/force-download/

    From my experience it doesn't always work. Some browsers will recognize it as xml and display it anyway, but it's probably the easiest way to do it.
     
    jestep, Aug 16, 2007 IP
  3. jazz7620

    jazz7620 Banned

    Messages:
    357
    Likes Received:
    12
    Best Answers:
    0
    Trophy Points:
    0
    #3
    I wrote a small script myself to create RSS feed..(xml)

    $articles1 = q($sql1);

    $outArticle = '<?xml version="1.0" encoding="UTF-8" ?>';
    $outArticle .= "\n";
    $outArticle .= '<rss version="2.0">';
    $outArticle .= "\n";
    $outArticle .= "\t<channel>\n";
    $outArticle .= "\t\t<title>YOUR WEBSITE TITLE</title>\n";
    $outArticle .= "\t\t<link>YOUR WEBSITE rxx FRRD URL</link>\n";
    $outArticle .= "\t\t<description>SOME DESCRIPTION</description>\n";
    $outArticle .= "\t\t<language>en-us</language>\n";


    while ($article1 = f($articles1))
    {


    $outArticle .= "\t\t<item>\n";
    $outArticle .= "\t\t\t<title>" .strip_tags(str_replace("&","and",trim($article1["Merchant_Name"]))) ." Coupons and deals</title>\n";
    $outArticle .= "\t\t\t<link>http://www.UsaDealsClub.com/" .trim($article1["Merchant_Name_Dash"]) .".html</link>\n";
    $outArticle .= "\t\t\t<description>" .strip_tags(str_replace("&","and",trim($article1["description"]))) ."</description>\n";
    $outArticle .= "\t\t</item>\n";

    }

    //print_r($slot);

    //echo $output;

    $outArticle .= "\t</channel>\n";
    $outArticle .= "</rss>";



    then write it to an output file

    $outfile = fopen($file,"w");
    fwrite($outfile,$outArticle);
    fclose($outfile);
     
    jazz7620, Aug 16, 2007 IP
    timallard likes this.
  4. timallard

    timallard Well-Known Member

    Messages:
    1,634
    Likes Received:
    62
    Best Answers:
    0
    Trophy Points:
    158
    #4
    Hello and thank you for this response!

    I have implemented your suggestion. I have worked things a little different, I am not generating any errors, but im not getting a file created. any ideas?

    this is a segment of my code:

    <?php
    $xmlFileName = $_POST['$xmlFileName'];

    $xml_doc= print "<br />";
    $xml_doc .= print "Welcome";

    $default_dir = "xml_files/";
    $default_dir .= $xmlFileName .".xml";

    $fp = fopen($default_dir, 'w');
    $write = fwrite($fp,$xml_doc);
    ?>


    i got an unexpected T_Echo when i used echo, so i tired to print...and i didnt get an error, but i also didnt get a file... -shrugs- thanks for the help!
     
    timallard, Aug 20, 2007 IP
  5. jazz7620

    jazz7620 Banned

    Messages:
    357
    Likes Received:
    12
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Most likely the directory you are trying to create file in does not have permission.

    Try this:
    1. Change the permission on the directory to 666 (if you are on linux/bsd) or make it writable.

    2. Create an empty file with the file name. Then try it.
    If this works then you would need to do this before tying to write to the file. This will check if a file already exist with the name, if not then it will create an empty file.

    if ( !file_exists($file))
    {
    touch ($file);
    }

    chmod($file,0777);

    Pls let me know how it works out for you. Cheers..!!
     
    jazz7620, Aug 20, 2007 IP
  6. timallard

    timallard Well-Known Member

    Messages:
    1,634
    Likes Received:
    62
    Best Answers:
    0
    Trophy Points:
    158
    #6
    Thank you! ok progress is being made..........

    i have this placed on a server,...when i try to chmod it manually i get a server error ( i might need to call host)

    so i tried it on my personal hosting.
    Its working up to creating a file.

    This is good, as its creating a file based on the name I specify,..but now i need to figure out why its filling it with "111111" ....hmmm
     
    timallard, Aug 20, 2007 IP
  7. timallard

    timallard Well-Known Member

    Messages:
    1,634
    Likes Received:
    62
    Best Answers:
    0
    Trophy Points:
    158
    #7
    here is my actual code...

    $xmlFileName = $txtQuizName;

    $xml_doc= print "<br />";
    $xml_doc .= print "&nbsp;" . "&nbsp;" . "&nbsp;" . "&nbsp;" . "&nbsp;" . "&nbsp;" . "&nbsp;"; $xml_doc .= print ("&lt;" . "/" . "items" . "&gt;" );

    $xml_doc .= print "<br />";
    $xml_doc .= print "&nbsp;" . "&nbsp;" . "&nbsp;" . "&nbsp;"; $xml_doc .= print ("&lt;" . "/" . "quiz" . "&gt;" );

    $default_dir = "xml_files/";
    $default_dir .= $xmlFileName .".xml";

    $fp = fopen($default_dir,'w') or die("Could not write file, oh no ughhh");
    $write = fwrite($fp,$xml_doc);

    echo $xml_doc;


    ------

    It seems as though every $XML_doc .= i have, an extra 1 gets added and not the value its storing...hmm
     
    timallard, Aug 20, 2007 IP
  8. timallard

    timallard Well-Known Member

    Messages:
    1,634
    Likes Received:
    62
    Best Answers:
    0
    Trophy Points:
    158
    #8
    I think i solved this,...im trying to combine 2 thing here hwne in fact i should be doing only one..


    my code is trying to output for display and throw it into an XML file, where I shold reformat my code to get rid of the format for display part and just put it in the xml.

    ill let you know how it goes, but I got rid of the prints and it did what it should.

    I do have 1 last question (i think) how can i add formatting to the xml doc to say, line break? wont BR get added to it visually as in <br>?
     
    timallard, Aug 20, 2007 IP
  9. timallard

    timallard Well-Known Member

    Messages:
    1,634
    Likes Received:
    62
    Best Answers:
    0
    Trophy Points:
    158
    #9
    Figured it out, thanks for all your help, i truly appreciate it!
     
    timallard, Aug 20, 2007 IP
  10. timallard

    timallard Well-Known Member

    Messages:
    1,634
    Likes Received:
    62
    Best Answers:
    0
    Trophy Points:
    158
    #10
    I ran into 1 more issue tho!!

    my xml export involves a loop..and its getting hung up on the while statement

    e.g. xml_doc .= while ( Question1 <= $howMany) {;



    hmmm
     
    timallard, Aug 20, 2007 IP
  11. jazz7620

    jazz7620 Banned

    Messages:
    357
    Likes Received:
    12
    Best Answers:
    0
    Trophy Points:
    0
    #11
    why don't you have another variable to be manipulated by the while loop and then assign that variable's value to xml_doc
     
    jazz7620, Aug 20, 2007 IP
  12. timallard

    timallard Well-Known Member

    Messages:
    1,634
    Likes Received:
    62
    Best Answers:
    0
    Trophy Points:
    158
    #12
    I think im stuck on the syntax of doing a loop within a variable..

    im getting a T_String error
     
    timallard, Aug 20, 2007 IP
  13. timallard

    timallard Well-Known Member

    Messages:
    1,634
    Likes Received:
    62
    Best Answers:
    0
    Trophy Points:
    158
    #13
    just threw it in my previous loop and it solved my issue. Thanks for the help again. im getting much better at this stuff! slowly but surely.
     
    timallard, Aug 20, 2007 IP