How can I echo the whole content of a .html file in PHP?
You should use readfile()
:
readfile("/path/to/file");
This will read the file and send it to the browser in one command. This is essentially the same as:
echo file_get_contents("/path/to/file");
except that file_get_contents()
may cause the script to crash for large files, while readfile()
won't.
Include whole content of a file and echo it
Just do:
include("http://www.mysite.com/script.php");
Or:
echo file_get_contents("http://www.mysite.com/script.php");
Notes:
- This may slow down your page due to network latency or if the other server is slow.
- This requires
allow_url_fopen
to be on for your PHP installation. Some hosts turn it off. - This will not give you the PHP code, it'll give you the HTML/text output.
How would you echo the content of a file in php?
Try something like:
<?php
$whitelistedKeys = array(
'Key', 'key1'
);
$input = $_GET['key'];
if (in_array($input, $whitelistedKeys, TRUE)) {
echo 'Whitelisted<br>';
$filePath = __DIR__ . '/my-file.txt';
echo "Content of \"$filePath\" file is:<br>";
echo file_get_contents($filePath);
} else {
echo 'Not Whitelisted';
}
?>
Note that I am not using double-quote to improve performance.
And am using
__DIR__
to loadmy-file.txt
from same directory which the PHP-script is in.
PHP echo HTML file
What do you get if you do:
var_export(get_magic_quotes_gpc());
Is it on? (true)
If so:
http://www.php.net/manual/en/security.magicquotes.disabling.php
or
$response = stripslashes(file_get_contents("second.html"));
How can I echo HTML in PHP?
There are a few ways to echo HTML in PHP.
1. In between PHP tags
<?php if(condition){ ?>
<!-- HTML here -->
<?php } ?>
2. In an echo
if(condition){
echo "HTML here";
}
With echos, if you wish to use double quotes in your HTML you must use single quote echos like so:
echo '<input type="text">';
Or you can escape them like so:
echo "<input type=\"text\">";
3. Heredocs
4. Nowdocs (as of PHP 5.3.0)
Template engines are used for using PHP in documents that contain mostly HTML. In fact, PHP's original purpose was to be a templating language. That's why with PHP you can use things like short tags to echo variables (e.g. <?=$someVariable?>
).
There are other template engines (such as Smarty, Twig, etc.) that make the syntax even more concise (e.g. {{someVariable}}
).
The primary benefit of using a template engine is keeping the design (presentation logic) separate from the coding (business logic). It also makes the code cleaner and easier to maintain in the long run.
If you have any more questions feel free to leave a comment.
Further reading is available on these things in the PHP documentation.
NOTE: PHP short tags <?
and ?>
are discouraged because they are only available if enabled with short_open_tag
php.ini configuration file directive, or if PHP was configured with the --enable-short-tags
option. They are available, regardless of settings from 5.4 onwards.
How to echo pieces of html from an external file on same folder?
Use includes
<html>
<head>
</head>
<body>
<?php
include 'header.html';
include 'container.html';
include 'footer.html';
?>
</body>
</html>
http://php.net/manual/en/function.include.php
PHP file content echo replace by text and positioning it somewhere in HTML page
Add an echo
statement that displays an image that depends on the state of the LED.
You can also simplify the code by noticing all the repetition and using a variable
$fileName = __DIR__.'/txt/led2.txt';
if (file_exists($fileName)) {
$current_state = file_get_contents($fileName);
if ($current_state != "0" && $current_state != "1") {
file_put_contents($fileName, '1');
$current_state = 1;
}
} else {
$current_state = 1;
file_put_contents($fileName, $current_state);
}
if (isset($_GET['on4'])) {
shell_exec("/usr/local/bin/gpio -g write 15 $current_state");
$current_state = 1 - $current_state;
file_put_contents($fileName, $current_state);
}
echo $current_state == 1 ? '<img src="images/ledOff.png">' : '<img src="images/ledOn.png">';
How do I get the HTML code of a web page in PHP?
If your PHP server allows url fopen wrappers then the simplest way is:
$html = file_get_contents('https://stackoverflow.com/questions/ask');
If you need more control then you should look at the cURL functions:
$c = curl_init('https://stackoverflow.com/questions/ask');
curl_setopt($c, CURLOPT_RETURNTRANSFER, true);
//curl_setopt(... other options you want...)
$html = curl_exec($c);
if (curl_error($c))
die(curl_error($c));
// Get the status code
$status = curl_getinfo($c, CURLINFO_HTTP_CODE);
curl_close($c);
Related Topics
Getting All Values from H1 Tags Using PHP
Deny Ajax File Access Using Htaccess
Malformed Utf-8 Characters, Possibly Incorrectly Encoded' in Laravel
How to Use In_Array If the Needle Is an Array
How to Add Extra Whitespace in PHP
Count the Number of Pages in a PDF in Only PHP
How to Pass Objects by Reference in PHP 5
Refresh a Table with Jquery/Ajax Every 5 Seconds
Codeigniter Send Email with Attach File
Woocommerce: Check If Items Are Already in Cart
$Stmt->Execute():How to Know If Db Insert Was Successful
How to Replace All Xhtml/HTML Line Breaks (<Br>) with New Lines
Select Random File from Directory
Creating an Array from a String Separated by Spaces
Get Mime Type of External File Using Curl and PHP