Reputation: 8488
I am trying to display the contents of a .cpp file in php. I am loading it using fread when I print it out it comes out formatted incorrectly. How can I keep the format without escaping each character?
Upvotes: 0
Views: 668
Reputation: 351
<?php
echo "<pre><code>";
$filename = "./test.cpp";
$handle = fopen($filename, "r");
if ($handle) {
while (!feof($handle)) {
$buffer = fgets($handle, 4096); // assuming max line len is 4096.
echo htmlspecialchars($buffer);
}
fclose($handle);
}
echo "</code></pre>";
?>
We need htmlspecialchars function to print it out correctly.
Upvotes: 1
Reputation: 522597
Assuming you want to look at it in a web browser:
<pre>
<code>
<?php echo htmlspecialchars(file_get_contents($file)); ?>
</code>
</pre>
Upvotes: 4