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.