Search code examples
phphtmlfile-get-contents

File content not getting displayed correctly in div


I'm trying to display the content of a php file to a div.

<div contenteditable="true" id="highlight">
<?php
echo file_get_contents( "/path/test.php" );
?>
</div>

test.php

<?php
require_once (lib.php');

/*
some comments here
*/
session_cache_limiter('private, must-revalidate');
header('Pragma: public');
@ob_start();
@session_start();

error_reporting(E_ALL);
ini_set('display_errors', '1'); 

// INCLUDES
define('FPDF_FONTPATH', "path/font/");
// print_r($_GET);
?>
.......................

But the content in the div is displaying from this line only : // print_r($_GET);. I need to display the entire content of the file.

Can anyone help me to fix this? Thanks in advance.


Solution

  • Because the HTML parser of your browser sees <?php as an HTML element it will be commented out. You have to convert < and > to their equivalent HTML entities.

    You can do this using htmlspecialchars (using <pre></pre> to keep the new lines from the actual file):

    <div contenteditable="true" id="highlight">
        <pre><?php // this should be directly after pre to prevent white-space from appearing before the code from the file
            echo htmlspecialchars(file_get_contents( "43677696_test.php" ));
        ?>
        </pre>
    </div>
    

    You can see the output here.