2

As the title implies, 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. So it needs to be read and printed in plaintext. The file that I'm trying to read is a PHP file. The HTML will be on the same server as the file to be read.

Jshaps
  • 59
  • 1
  • 5

3 Answers3

2

To display the contents of any file with PHP you can do this:

 echo htmlspecialchars(file_get_contents($filename));

The variable $filename is a local filesystem path (not a URL), so it doesn't matter if the file is accessible through the web. The only restriction is that the PHP process needs to have read access to the file.

Note that htmlspecialchars should be told what the doctype and the encoding of your page is using 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');
Jon
  • 428,835
  • 81
  • 738
  • 806
  • Isn't this for use in a PHP file? To reiterate, I have to use exclusively HTML for this. – Jshaps Jan 21 '13 at 18:46
  • @Jshaps: That is not possible unless the file is accessible from the web (which you say it's not). – Jon Jan 21 '13 at 18:49
  • Ah, so it's not possible? The HTML cann't read serverside files? – Jshaps Jan 21 '13 at 18:50
  • @Jshaps: No. HTML is not a general-purpose programming language. – Jon Jan 21 '13 at 18:51
  • @Jon See http://stackoverflow.com/a/14532168/427545 for why ENT_HTML5 is useless when using htmlspecialchars. I suggest to use `htmlspecialchars(..., ENT_NOQUOTES)` here, all other variants are redundant for this specific code. – Lekensteyn Jan 26 '13 at 00:21
1

If you want it nicely formatted, see highlight_file():

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

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

To do this in plain html, you'll need to use javascript and fire an ajax request to get that file. But you still need to rename your script to .phps, .txt or something that 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)

nice ass
  • 16,471
  • 7
  • 50
  • 89
0

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

There is also no "server side" with HTML. Everything's client side. There is no processing of your code, just rendering of your page (by your client's browser).

That said, you cannot read a file using HTML. You can, though, render the content of another file using an iframe.

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

You cannot 'read' it and process that file using HTML alone. You can't do this even with javascript (which is a programming language, but can't access other files).

You could, as suggested on other answers here, use javascript to dispatch ajax calls, have your file read on the server (by a PHP script, for example), and have the value returned to your client side (and process the result with javascript).

Pedro Cordeiro
  • 2,085
  • 1
  • 20
  • 41