Jump to content

Recommended Posts

Hey. I'm adding an RSS feature to my mini CMS type thing and its supposed to read the previous entries from a buffer that is a text file, add the new entry to that file and save the new entry, followed by the old entries into one variable. Then its supposed to write that variable (with all posts, newest at the top) to an xml file with a header and footer before and after that entries. The buffer is working fine but the feed.xml is blank. However it does seem to create a feedxml file and when i open it with notepad or wordpad it has all the content that I would expect in the feed.xml. Can anyone explain why this is happening? Heres my code that does this:

 

The variable $rss is just the entries, which would normally have variables for the title and description but I've hard coded them here for you.

 

 

$rss = "<item> \n<title>Wiiiii</title> \n<description>This is a test entry.</description> \n <link>www.site.com/test.php</link> \n </item>";

 

//checks if the file is already listed in the all file and adds it if not

if (!file_exists("rssBuffer.txt")){ 
touch("rssBuffer.txt");
}

$fh = fopen("rssBuffer.txt", 'r');
$fold = fread($fh, filesize("rssBuffer.txt"));

$write = "$rss \n $fold";

$fh2 = fopen("rssBuffer.txt", 'w+');
fwrite($fh2, $write);
fclose($fh);
fclose($fh2);

//end adding the rss to the buffer


$header = "<?xml version=\"1.0\"?> \n<rss version=\"2.0\"> \n<channel> \n";
$footer = "</channel> \n</rss>";
$fullRSS = $header.$write.$footer;
//add feed to the rss

if (!file_exists("feed.xml")){ 
touch("feed.xml");
}

$fh = fopen(feed.xml, 'w');
if($fh==false)
die("unable to create file");
fwrite($fh, $fullRSS);

fclose($fh);


//end adding feed to the rss

Link to comment
https://forums.phpfreaks.com/topic/114846-solved-creating-xml-file-with-php/
Share on other sites

If I understand correctly, it is generating a file on your server named "feedxml" but not correctly placing the contents (that you *are finding in "feedxml") into "feed.xml"?

 

If so, and if the code sample you provided is something like what you're using, the problem is right here:

 

$fh = fopen(feed.xml, 'w');

 

If you were showing All Errors (or if you go and check your logs), you would most likely see two NOTICEs happening on this line, something along the lines of "Undefined Constant".

 

The fix is the put quotes around "feed.xml".

 

As PHP sees it right now, it tries to find a Constant called 'feed', and not finding one it uses the string "feed".  It doesn't use the dot, because now it thinks it's supposed to concatenate with what comes next (since the period isn't in quotes of any kind) ... then it sees "xml" and once again doesn't find a constant named "xml" so it uses the string "xml".

 

The period, not being in a string, concatenates the two strings, and you get "feedxml".

 

 

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.