GreenMarch Posted November 27, 2007 Share Posted November 27, 2007 Can anyone give me a pointer please, I'm new at this but keen to learn: I am converting text to html from an uploaded file, applying some CSS then writing the html to the top of an Existing file which is called as an include on a web page. Code is: <?php $source = "plain.txt"; $raw = file($source) or die("Cannot read file"); $slug = array_shift($raw); $byline = array_shift($raw); $data = join('', $raw); $html = nl2br(htmlspecialchars($data)); $html = preg_replace('/\s\s+/', ' ', $html); $output .= "<div class='newentry'>"; $output .= "<div class='slug'>$slug</div>"; $output .= "<div class='byline'>$byline</div>"; $output .= "<div class='text'>$html</div>"; $output .="</div>"; $handle = fopen("reports.php", "r+"); $filename = 'reports.php'; $somecontent = "$output"; if (is_writable($filename)) { if (!$handle = fopen($filename, 'r+')) { echo "Cannot open file ($filename)"; exit; } if (fwrite($handle, $somecontent) === FALSE) { echo "Cannot write to file ($filename)"; exit; } echo "Success, wrote to file ($filename)"; fclose($handle); } else { echo "The file $filename is not writable"; } ?> This all works Except it overwrites what is already in the reports.php file instead of placing new html at top which the r+ mode says should be happening. I dare say the code is the most inelegant you've seen so if anyone cares to help with problem and suggest, neater more efficient execution, I'd be very grateful. Thanks in advance. Quote Link to comment Share on other sites More sharing options...
Wes1890 Posted November 27, 2007 Share Posted November 27, 2007 This could be a problem: $handle = fopen("reports.php", "r+"); $filename = 'reports.php'; // HERE $somecontent = "$output"; /// Change that line to: $somecontent = $output; I'm not saying thats the big problem, but that is certainly an invalid line. Quote Link to comment Share on other sites More sharing options...
GreenMarch Posted November 27, 2007 Author Share Posted November 27, 2007 Thanks Wes1890 I can see my error but result is same except it leaves div> on page after output viewed, so presumably has chopped all but closing in newentry class. Quote Link to comment Share on other sites More sharing options...
tbare Posted November 27, 2007 Share Posted November 27, 2007 so, you are wanting to add information to the txt file, but instead of adding, it's overwriting? Quote Link to comment Share on other sites More sharing options...
GreenMarch Posted November 27, 2007 Author Share Posted November 27, 2007 Yes tbare that's right - which is what the r+ mode should do. Quote Link to comment Share on other sites More sharing options...
tbare Posted November 27, 2007 Share Posted November 27, 2007 <?php $newsfile = "news.txt"; $fh = fopen($newsfile , 'r'); //opens news.txt for reading $news = $_POST['news']; //gets information from post $news = str_replace(array("\r\n", "\r", "\n"), "", $news); // removes carriage returns at the end of the lines if (!file_exists("news.txt")) touch("news.txt"); $fcontent = fread($fh, filesize("news.txt")); //copies all data from news.txt $towrite = "$fcontent"; $fh2 = fopen('news.txt','w+'); //opens file to be written to fwrite($fh2,$news); //adds new content to file fwrite($fh2,$towrite); //adds old content to file fclose($fh); //closes file fclose($fh2); //closes file print "Yay!! News file successfully updated.<br>"; ?> here's how i did it from a POST to get it to append the new content to the top of the file... (r+ didn't seem to work for me). hope this helps Quote Link to comment Share on other sites More sharing options...
GreenMarch Posted November 27, 2007 Author Share Posted November 27, 2007 Thanks again tbare. I was wondering if _POST could be used to provide the update but thought this purely for mail. How does that work? Will try and apply your "open to read and copy - open to write - writenew - writeold - close" to my task. Quote Link to comment Share on other sites More sharing options...
tbare Posted November 27, 2007 Share Posted November 27, 2007 here's how i did... i'm sure there's a much easier way, but this worked for me: <?php print "<form action='news2.php' method='POST'>\n"; print "<textarea NAME='news' ROWS=10 COLS=75>Insert News Here</textarea><br>\n"; print "<input type=submit value=Submit> <input type=reset value=Reset>\n"; print "</form>\n"; ?> where news2.php contains the code: <?php $newsfile = "news.txt"; $fh = fopen($newsfile , 'r'); //opens news.txt for reading $news = $_POST['news']; //gets information from post $news = str_replace(array("\r\n", "\r", "\n"), "", $news); // removes carriage returns at the end of the lines if (!file_exists("news.txt")) touch("news.txt"); $fcontent = fread($fh, filesize("news.txt")); //copies all data from news.txt $towrite = "$fcontent"; $fh2 = fopen('news.txt','w+'); //opens file to be written to fwrite($fh2,$news); //adds new content to file fwrite($fh2,$towrite); //adds old content to file fclose($fh); //closes file fclose($fh2); //closes file print "Yay!! News file successfully updated.<br>"; ?> Quote Link to comment Share on other sites More sharing options...
GreenMarch Posted November 28, 2007 Author Share Posted November 28, 2007 Hi tbare Thanks to you and others on a couple of other forums I have solved the problem completely: The solution is: <?php $source = "plain.txt"; $raw = file($source) or die("Cannot read file"); $slug = array_shift($raw); $byline = array_shift($raw); $data = join('', $raw); $html = htmlspecialchars($data); $html = preg_replace('/\n\n/', '<br /><br />', $html); //This preserves paragraph breaks $html = preg_replace('/\n/', '', $html); $html = preg_replace('/\s\s+/', ' ', $html); $output .= "<div class='newentry'>"; $output .= "<div class='slug'>$slug</div>"; $output .= "<div class='byline'>$byline</div>"; $output .= "<p>$html</p>"; $output .="</div>"; $reportfile = "reports.php"; $fh = fopen($reportfile , 'r'); //opens reports.php for reading $news = $output; //gets the new data if (!file_exists("reports.php")) touch("reports.php"); $fcontent = fread($fh, filesize("reports.php")); //copies all data from reports.php $towrite = "$fcontent"; $fh2 = fopen('reports.php','w+'); //opens file to be written to fwrite($fh2,$news); //adds new data to file fwrite($fh2,$towrite); //adds old content to file fclose($fh); //closes file fclose($fh2); //closes file ?> Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.