Reading and displaying file content on the server side using only HTML

As the name suggests, I need a way to display a file on a website without using PHP. The file cannot be accessed directly, it just shows a blank page. Therefore, it must be read and printed in clear text. The file I am trying to read is a PHP file. The HTML will be on the same server as the file to be read.

+3


source to share


3 answers


To display the content of any file using PHP, you can do this:

 echo htmlspecialchars(file_get_contents($filename));

      

The variable $filename

is the local file system path (not a URL), so it doesn't matter if the file is accessible over the Internet. The only limitation is that the PHP process must have read access to the file.



Note that it htmlspecialchars

must be stated that the doctype and encoding of your page use its second and third arguments. For example:

 header('Content-Type: text/html; charset=utf-8'); // UTF-8
 echo '<!DOCTYPE html>'; // HTML 5
 echo htmlspecialchars(file_get_contents($filename), ENT_HTML5, 'UTF-8');

      

+2


source


If you want it to be well formatted see highlight_file()

:

print highlight_file('path/to/your/file.php');

      

(I also wrote a function that does this a little better)

Strike>



To do this in normal html, you will need to use javascript and run an ajax request to get this file. But you still need to rename your script to .phps

, .txt

or something PHP won't try to parse.

Example using jQuery:

<html>
<head>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script>
  jQuery(document).ready(function($){
    $.ajax({
        url : "file.phps",
        dataType: "text",
        success : function (data) {
           $('<pre />').text(data).appendTo('body');
        }
    });
  });
</script> 
</head>    
<body>       
</body>  

      

( file.phps

must exist on your server)

+1


source


You clearly don't understand the purpose of HTML - it is not a programming language, but a markup language designed for layouts.

There is also a "server side" with HTML. All client sides. Your code is not processed, but your page is just displayed (by your client browser).

However, you cannot read the file using HTML. However, you can display the content of another file using an iframe.

<iframe src="myOtherFile.txt"></iframe>

      

You cannot "read" it and process this file using only HTML. You cannot even do this with javascript (which is a programming language, but cannot access other files).

You could, as suggested in other answers here, use javascript to send ajax calls, for example your file is read on the server (like a PHP script) and has a value returned to your client side (and handle the result with javascript).

0


source







All Articles