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?
This is what I needed. But how do I get it to display The #include <stdio.h> correctly?
DHamrick
2009-07-10 05:32:15
all it displays is #include and then skips the <stdio.h> part
DHamrick
2009-07-10 05:35:20
<pre>#include <stdio.h></pre>
John T
2009-07-10 05:36:18
+4
A:
Assuming you want to look at it in a web browser:
<pre>
<code>
<?php echo htmlspecialchars(file_get_contents($file)); ?>
</code>
</pre>
deceze
2009-07-10 05:28:15
+1
A:
<?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.
antreality
2009-07-10 05:48:50