Jump to content

[SOLVED] fopen (r+) problem


GreenMarch

Recommended Posts

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.

Link to comment
Share on other sites

<?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

Link to comment
Share on other sites

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>";
?>

Link to comment
Share on other sites

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 

?> 

Link to comment
Share on other sites

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.