File content not getting displayed correctly in div - php

I'm trying to display the content of a php file to a div.
<div contenteditable="true" id="highlight">
<?php
echo file_get_contents( "/path/test.php" );
?>
</div>
test.php
<?php
require_once (lib.php');
/*
some comments here
*/
session_cache_limiter('private, must-revalidate');
header('Pragma: public');
#ob_start();
#session_start();
error_reporting(E_ALL);
ini_set('display_errors', '1');
// INCLUDES
define('FPDF_FONTPATH', "path/font/");
// print_r($_GET);
?>
.......................
But the content in the div is displaying from this line only : // print_r($_GET);. I need to display the entire content of the file.
Can anyone help me to fix this? Thanks in advance.

Because the HTML parser of your browser sees <?php as an HTML element it will be commented out. You have to convert < and > to their equivalent HTML entities.
You can do this using htmlspecialchars (using <pre></pre> to keep the new lines from the actual file):
<div contenteditable="true" id="highlight">
<pre><?php // this should be directly after pre to prevent white-space from appearing before the code from the file
echo htmlspecialchars(file_get_contents( "43677696_test.php" ));
?>
</pre>
</div>
You can see the output here.

Related

Set backgroung image in .php file

I downloaded this code:
$image = ImageClass::getImage('bg.jpeg','myTitle');
$bg_img = explode(" ",$image);
$src = substr(strpos('"',$bg_img),strlen($bg_image)-1);
echo "<div style='background-image: url(".$src.");' ></div>
<?php
/*
*** OPTIONS ***/
// TITLE OF PAGE
$title = "ARQUIVOS PROPAR";
// STYLING (light or dark)
$color = "dark";
// ADD SPECIFIC FILES YOU WANT TO IGNORE HERE
$ignore_file_list = array( ".htaccess", "Thumbs.db", ".DS_Store", "index.php", "flat.png", "error_log" );
// ADD SPECIFIC FILE EXTENSIONS YOU WANT TO IGNORE HERE, EXAMPLE: array('psd','jpg','jpeg')
$ignore_ext_list = array( );
// SORT BY
$sort_by = "name_asc"; // options: name_asc, name_desc, date_asc, date_desc
// ICON URL
//$icon_url = "https://www.dropbox.com/s/lzxi5abx2gaj84q/flat.png?dl=0"; // DIRECT LINK
$icon_url = "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAA+gAAAAyCAYAAADP7vEwAAAgAElEQVR4nOy9d5hdV3nv";
// TOGGLE SUB FOLDERS, SET TO false IF YOU WANT OFF
$toggle_sub_folders = true;
// FORCE DOWNLOAD ATTRIBUTE
$force_download = true;
// IGNORE EMPTY FOLDERS
$ignore_empty_folders = false;
// SET TITLE BASED ON FOLDER NAME, IF NOT SET ABOVE
if( !$title ) { $title = clean_title(basename(dirname(__FILE__))); }
?>
Th full code can be download here: https://github.com/halgatewood/file-directory-list/blob/master/index.php
I'm having problem with the start:
$image = ImageClass::getImage('bg.jpeg','myTitle');
$bg_img = explode(" ",$image);
$src = substr(strpos('"',$bg_img),strlen($bg_image)-1);
echo "<div style='background-image: url(".$src.");' ></div>
I want to put a picture as background, but it isn't happening. What's wrong?
Changed with the answer:
<?php
echo "<div style='background-image: url('/bg.jpeg');' ></div>";
?>
<?php
/*
*** OPTIONS ***/
// TITLE OF PAGE
$title = "ARQUIVOS PROPAR";
// STYLING (light or dark)
$color = "dark";
etc..
No need for all that,
What you desire to achieve is much simpler.
Assuming this code is inside index.php and your server's directory structure:
/some-folder/
/index.php
/bg.jpeg
Simply link it as its done in plain html —
<?php
echo "<div style=\"background-image: url('/bg.jpeg');\" ></div>";
?>
If you wan't it do be dynamic, i.e, image files's name is inside a variable then,
<?php
$my_image = 'bg.jpeg';
echo "<div style='background-image: url($my_image);' ></div>";
?>
Update:
Important Tip: All programming languages are executed line-by-line, this tip applies not only to PHP, but also HTML Learn More
Assume for example, your page's html structure returned to the browser is as provide below and you want to apply background to body tag
<html>
<head><head>
<body>
<nav>Some dummy navigation</nav>
<div>welcome to my website</div>
<footer>Copyright</footer>
</body>
</html>
Simply copying and pasting my code to the top of page will result in
<div style="background-image: url('bg.jpeg');" ></div>
<html>
<head><head>
<body>
<nav>Some dummy navigation</nav>
<div>welcome to my website</div>
<footer>Copyright</footer>
</body>
</html>
But that created a empty div tag at the top of html output, i wanted it to apply background to by body tag instead !!!?
— This happened because echo is used to send output to the browser as soon as it is executed. So since you copied my code to the top of your script the html output is also at the top.
But why did it echo <div style="background-image: url('bg.jpeg');" ></div> when i wanted it to apply to my page's body?
— Because the echo statements reads "<div style=\"background-image: url('bg.jpeg');\" ></div>"; as its output.
Ok, but how to apply the background-image to body then??
As mentioned earlier code is executed line-by-line, so in order to apply the style to pages's body tag you'll need to call it near your body tag and also modify it to not output the div it currently does.
So assuming your index.php is:
<?php
$my_image = 'bg.jpeg';
echo "<div style='background-image: url($my_image);' ></div>";
?>
<html>
<head><head>
<body>
<nav>Some dummy navigation</nav>
<div>welcome to my website</div>
<footer>Copyright</footer>
</body>
</html>
You'll need to change it to —
<?php
$my_image = 'bg.jpeg';
// don't echo any thing here
?>
<html>
<head><head>
<body style="background-image: url('<?php echo $my_image; ?>')">
<!-- apply the style to body -->
<nav>Some dummy navigation</nav>
<div>welcome to my website</div>
<footer>Copyright</footer>
</body>
</html>
Hopefully i explained it well :)

How to convert php code to html source code?

I need to display html source code form other php file.
I have two file
code.php
index.php (I hope I can convert the code.php to html source code.)
code.php:
<!DOCTYPE html> <html> <body> <?php $color = "red"; echo $color; ?> </body> </html>
index.php (I hope I can convert the code.php to html source code.)
$php_to_html = file_get_contents("code.php");
$html_encoded = htmlentities($php_to_html);
echo $html_encoded;
but when i run the index.php file, the result is
<!DOCTYPE html> <html> <body> <?php $color = "red"; echo $color; ?> </body> </html>
but I hope I can see the result is
<!DOCTYPE html> <html> <body> red </body> </html>
any idea how can i do this ,thanks!!!
You want to execute the PHP, so include it and capture the output:
ob_start();
include("code.php");
$php_to_html = ob_get_clean();
$html_encoded = htmlentities($php_to_html);
echo $html_encoded;
If you want the HTML to be rendered as HTML then don't use htmlentities().
Optionally (not the best way) but you can execute it by retrieving from the URL:
$php_to_html = file_get_contents("http://www.example.com/code.php");
$html_encoded = htmlentities($php_to_html);
echo $html_encoded;
Buffer output and include it:
ob_start();
include_once('code.php');
$html = ob_get_clean();
By using output buffering, any output is not sent to the browser, but instead kept in memory. This allows you to run the code, and get the output as a variable. ob_get_clean() flushes the buffer (in this case into our $html variable), and then stops buffering, allowing you to continue as normal. :-)

How to convert an HTML page to PDF in PHP

<?php
include ('mpdf/mpdf.php');
$mpdf = new mpdf;
ob_start();
?>
<html>
<head></head>
<body>
My various content with table, dropdown menu and 2 include files.
</body>
</html>
<?php
$html = ob_get_contents();
ob_end_clean();
$mpdf->Output();
exit;
?>
Wondering why this function doesn't work and will only output empty pdf file. I have tried many ways but it just doesn't work. I have placed this function at the beginning of my code but it always outputs an empty file.
Try this code:-
<?php
$html = ob_get_contents();
ob_end_clean();
// You need to write html
$mpdf->WriteHTML($html);
// define path of your output file and set mode 'F' for saving
$mpdf->Output('filename.pdf','F');
exit;
?>

how to get html source without any error?

i want get the html source of a page but give a error!!
i use this code for give:
<?php
header('Content-Type: text/html; charset=utf-8');
$html = file_get_html("http://www.google.com/");
echo $html;
when I want to get the source from here I don't correct response and I get something like these characters
����i�[S$%ٲ�9������
Use the function file_get_contents instead:
http://www.php.net/manual/en/function.file-get-contents.php
you should do like this,note: Download simple_html_dom class here
<?php
include_once('simple_html_dom.php');
$html = file_get_html('http://www.google.co.in');
echo $html;
?>

Php ob_get_contents fail

I am using ob_get_contents() to create a html file from php file. On development machine sometimes it works but on the test machine it did not work.
<html>
<body>
<div>
//some html content
</div>
</body>
</html>
<?php
ob_start();
file_put_contents('./pdfreportresult.html', ob_get_contents());
require_once (APP_DIR . 'assessment/wkhtmltopdf/snappy-master/src/autoload.php');
use Knp\Snappy\Pdf;
$snappy = new Pdf(APP_DIR . 'assessment/wkhtmltopdf/wkhtmltopdf.exe');
header('Content-Type: application/pdf');
header('Content-Disposition: attachment; filename="report.pdf"');
echo $snappy->getOutput(APP_DIR . 'assessment/pdfreportresult.html');
ob_end_clean();
?>
I checked test machine's php.ini for output_buffering and it is "On" like on development machine. When i checked my created html file "pdfreportresult.html" it is empty or half-content is exist.
Maybe issue can related with buffer size and i tried ob_clean() instead of ob_end_clean() still not working.
Start the buffer before you start outputting content. Also, clean the buffer once you're done with it.
<?php
ob_start();
?>
<html>
<body>
<div>
//some html content
</div>
</body>
</html>
<?php
file_put_contents('./pdfreportresult.html', ob_get_contents());
ob_end_clean();
...

Categories