PHP Show Source Code Instead of .html File - php

I'm sorry if the title is a bit confusing, but this is my problem:
I need to echo the source of a .html file using PHP. I tried using PHP include(); while the file's extensions were .html.txt, but that showed up as if it is . When I access the file directly, for example "example.com/file.html.txt", it shows up as a normal .txt.
This is the PHP code I'm using to display the file:
<div>
<?php
include "../head.php";
$path = $_GET["p"];
?>
<div style="position:absolute; top:90px; padding-left:5px; padding-right:10px;">
<?php
echo "<code>";
include("files/".$path);
echo "</code>";
?>
</div>
I hope I'm understandable! :) Thanks in advance for your help!

Don't use include(), read the contents of the file and print them instead (don't forget to escape).
In your case $path is user input, you should make sure that no sensitive informatin can be accessed. Using basename($path); makes sure that no directory traversal is possible.
<code>
<?php
// $path = basename($path);
$html = file_get_contents("files/".$path);
echo htmlspecialchars($html);
?>
</code>

If you want to output HTML tags in an html file, use htmlspecialchars() to escape special characters.

I'd suggest wrapping it inside a textarea. Simple and quick.
echo "<textarea>";
include "/filename.txt";
echo "</textarea>";
Sometimes, when we're looking for something very simple, we overlook the obvious. :o)

if you want to be able to see html source in a browser, the best method would be to run it through the function htmlentities() or like. the tricky part is getting the file contents of the include into a string so that you can do that. for this we use ob_start() and ob_get_clean(). these basically start an output buffer, then get the contents of the output buffer so you can process it and echo it.
<div>
<?php
include "../head.php";
$path = $_GET["p"];
?>
<div style="position:absolute; top:90px; padding-left:5px; padding-right:10px;">
<?php
echo "<code>";
ob_start();
include("files/".$path);
echo htmlentities(ob_get_clean());
echo "</code>";
?>
</div>

Include is not safe. If your file has PHP code it will be executed.
You should rather use this:
echo htmlspecialchars(file_get_contents('yourfile.html'));

Related

Is there a way to save regular HTML/CSS/JS as a php variable?

I am familiar with HEREDOC, but that is not what I want to use.
What I was hoping for was a way to edit HTML/CSS in the PHP file so that it is not technically a string so that I can get proper HTML/CSS/JS hints and autocompletes in my text editor. When trying to edit inside HEREDOC it is considered a string so I do not get any hits, which causes a lot of syntax errors.
Something like this: (Like the use of the if statement brackets)
<?php
$html='
?>
<style>
h4{
font-weight:700;
}
</style>
<h4>HTML</h4>
<?php
';
echo $html;
?>
Is there a way to put the html/css in another file then include it inside a concat string?
<?php
$html=''.include('html.php').'';
echo $html;
?>
You could use output buffering - in this simplified example you'd get the html from file view.php and you'd assign it to $html.
<?php
$html = getHtml('view.php');
echo $html;
function getHtml(string $file)
{
ob_start();
include $file;
return ob_get_clean();
}
ob_start() turns on output buffering
ob_get_clean() gets current buffer contents (returns as string) and deletes current output buffer
As far a PHP is concerned, HTML and JS is just text so you can store them in a PHP variable in the same way as you would store any string.
$html = '<style>
h4{
font-weight:700;
}
</style>
<h4>HTML</h4>';
To load HTML from a separate file into a variable, how you would do it depends on the file. If it contains PHP code that needs to be executed, then follow jibsteroos answer about output buffering.
If the file doesn't contain any PHP, you can use file_get_contents():
$html = file_get_contents('html.php');
Note: If html.php contains any PHP, it will be read as text instead of being executed.

How to printf an include html

I included an html file like this, so that it is not displayed when the site loads:
<div id="menugrp0" class="menuhide">
<?php include 'menugrp0.html'; ?>
</div>
Now I want it to be shown at a specific spot. I am using this php code, to get some variables which are transported with the $_SESSION. I am using this kind of question for some simple html links, in which case it works perfectly:
if ($_SESSION['gruppe'] == $h['gruppe']) {
printf(' menugrp0.html');
}
I know that this is not working at all at the moment for this included html. I also tried to add the <?php [...] ?> tag inside the printf, which is also not working.
Is it possible to show a hidden included html file with a printf tag?
Try this one.
<?php
if($_SESSION['gruppe'] == $h['gruppe']){
echo 'Foo';
include ('/path/to/menugrp0.html');
echo 'Example: one';
}
?>
readfile('menugrp0.html'); // Reads a file and writes it to the output buffer. It is like read then "echo"
How to echo the whole content of an .html file in php?
Thanks to Daan Meijer, this works.
echo file_get_contents('...');
}

PHP - How to echo a variable that contains php?

If I have a piece of code that reads a chunk of HTML from a txt file and then echos that html onto the page, how can I accomplish the same task, but when there is PHP inside of the txt file?
ex:
this is the file being read:
<?php
$filecontent = // read some other file
echo($filecontent);
?>
and this is the page that is reading the file:
<?php
$code1 = //reading the above file
?>
<html>
<?php echo($code1); ?>
</html>
When you want to process files containing PHP code you need to use include instead of echo.
<?php include('your_php_file_name'); ?>
If you have the contents of the file in a string you are in a tough spot because the only way to process the code is eval, and in addition you have to properly set up any environment that the code requires. eval itself should be avoided, and the latter is impossible to do in the general case.
Use include instead of echo:
<?php include($file_that_contains_php); ?>
you need to include the first file and echo statement in the first file will get executed.
<html>
<?php require_once("firstfile.php"); ?>
You need to echo htmlentities($code1), because when you echo then browser will not show it contents, because it try to parse it as a html tag, but htmlentities will encode to safe html output this characters.
If you want to evaulate the code, then you need eval($code1) or include it.

html in a php variable

I want to put some procedural stuff like html in a variable;
Normally we do this:
<?php
$my_var = '
<h1>some header</h1>
some text.
';
?>
Now I want it to look like this:
<?php
$my_var = '
?>
<h1>some header</h1>
some text.
<?php
';
?>
So that we open a php code, declare a variable, do some stuff outside php, and then reopen php code and close the var and we get that stuff that is outside php, we get it inside that variable.
Thanks.
<?php
ob_start();
?>
<h1>some header</h1>
some text.
<?php
$my_var = ob_get_contents();
ob_end_clean();
echo my_var;
?>
i don't think there is a way because php ignores lines outside the php tags.
if u have the php in a other file user file_get_contents
Please check php heredoc
http://php.net/manual/en/language.types.string.php
Hope it helps

Assigning HTML contents to PHP variables

Hard to explain, how to assign HTML contents to PHP variables. The HTML contents are not within the PHP script.as
<?php $a_div = ?><div>Contents goes <b>here</b></div>
Try with ob_get_contents: ob_get_contents() or ob_get_clean()
<?php ob_start(); ?>
<div>Contents goes <b>here</b></div>
<?php $contents = ob_get_contents(); ?>
If its raw HTML, with no PHP in it, stored in separate file it would be just
$a_div = file_get_contents('email.html');
If it's PHP file with HTML mixed with PHP then include it using output buffering like in Robik's answer.
ob_start();
include 'mail.template.php';
$a_div = ob_get_contents();
But if it's relatively small, I'd use HEREDOC
$a_div = <<<HERE
<div>Contents goes <b>here</b></div>
HERE;
If needed (normally I'm using html templates, replacing placeholders by php) ...
there are two ways:
$testVar = <<<EndOfHtml
<div style="border: 48px solid red">test</div>
EndOfHtml;
print $testVar
the easier way, not too elegant - wrap html code in single quotes. so you don't have to escape any double quote:
$test_var = '<div style="border: 48px solid red">test</div>'
helpful? :-)
Are you looking for token-terminated Block-Strings?
http://www.phpf1.com/tutorial/php-heredoc-syntax.html
Best I can do. Unfortunately your question is a little vague.
It's a bit late, and it's not exactly what was asked, but if you're looking to render the page out of order (such as with templating), one could also wrap that section with a function and call it to render it in the desired location.
<?php function RenderFooter() { ?>
<footer>some footer</footer>
<?php } ?>
<h1>Some header</h1>
<main>Some content</main>
<?= RenderFooter(); ?>
you can use DOMDocument To parse html file and get contents you need.

Categories