Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP - how to load HTML files? [duplicate]

Tags:

html

php

At the moment I have a file like this

<?php
if(some condition)
{
    //Dont allow access
}
else
{
    echo "<html>My HTML Code</html>";
}
?>

But I wanted to do something like this to keep my php file short and clean.

<?php
if(some condition)
{
    //Dont allow access
}
else
{
    //print the code from ..html/myFile.html
}
?>

How can I achieve this?

like image 732
Joe Slater Avatar asked Mar 07 '13 11:03

Joe Slater


4 Answers

Extending nauphal's answer for a more robust solution..

<?php
if(some condition)
{
    //Dont allow access
}
else
{
    if(file_exists("your_file.html"))
    {
       include "your_file.html";
    }
    else
    {
      echo 'Opps! File not found. Please check the path again';
    }
}
?>
like image 100
asprin Avatar answered Nov 15 '22 18:11

asprin


you may have a look at PHP Simple HTML DOM Parser, seems a good idea for your needs! Example:

// Create a DOM object from a string
$html = str_get_html('<html><body>Hello!</body></html>');

// Create a DOM object from a URL
$html = file_get_html('http://www.google.com/');

// Create a DOM object from a HTML file
$html = file_get_html('test.htm');
like image 26
Prijm.com Avatar answered Nov 15 '22 18:11

Prijm.com


save your html content as seperate template and simply include it

<?php
if(some condition)
{
    //Dont allow access
}
else
{
    include ("your_file.html");
}
?>

OR

<?php
if(some condition)
{
    //Dont allow access
}
else
{
    readfile("your_file.html");
}
?>

readfile is faster and less memory intensive than file_get_contents

like image 25
Nauphal Avatar answered Nov 15 '22 18:11

Nauphal


Use this code


if(some condition)
{
    //Dont allow access
}
else
{
    echo file_get_contents("your_file.html");
}

OR


if(some condition)
{
    //Dont allow access
}
else
{
    require_once("your_file.html");
}

like image 21
Tsimtsum Avatar answered Nov 15 '22 19:11

Tsimtsum