I have the following code:
<?
$serverurl = $_SERVER["DOCUMENT_ROOT"];
$file = $serverurl.'/demo/sample_php.php';
$newfile = $serverurl.'/demo/sample_php.txt';
if (!copy($file, $newfile)) {
echo "failed to copy $file...\n";
}
$homepage = file_get_contents($serverurl.'/demo/sample_php.txt');
?>
<pre class="code">
<code class="php boc-html-script">
<? echo htmlentities($homepage, ENT_QUOTES); ?>
</code>
</pre>
<? unlink($newfile); ?>
This basically copies a *.php file to a *.txt file, displays the contents, then deletes it. However, I don't want to create a visible file, as the application is designed to display a list of files, then display the contents of the file. Having a file appear with a .txt extension would be confusing.
I realize I could create a folder that is hidden, and do all my converting there, but I am thinking there must be a more efficient way to display the contents of a php file.
I did some experimenting with tmpfile(), but I couldn't get the contents of the php file to write to it.
Any ideas?
There is no reason to do the file copy. file_get_contents() returns the contents of the file as a string, which is all you need. It will not parse and execute the PHP code as include()/require() would.
Just retrieve the contents of the PHP file into the $homepage variable and echo it out as you have done with the temporary text file.
<?php
// get the PHP file directly.
$homepage = file_get_contents($serverurl.'/demo/sample_php.php');
?>
<pre class="code">
<code class="php boc-html-script">
<?php echo htmlentities($homepage, ENT_QUOTES); ?>
</code>
</pre>
After suggestions in the comments to print with highlighting, you can do it more easily with highlight_file():
highlight_file($serverurl.'/demo/sample_php.php');
Related
How to read a .php file using php
Let's say you have two files a.php and b.php on same folder.
Code on the file b.php
<?php
echo "hi";
?>
and code on a.php
<?php
$data = file_get_contents('b.php');
echo $data;
You access a.php on browser.
What do you see? A blank page.
Please check the page source now. It is there.
But not showing in browser as <?php is not a valid html tag. So browser can not render it properly to show as output.
<?php
$data = htmlentities(file_get_contents('b.php'));
echo $data;
Now you can see the output in browser.
If you want to get the content generated by PHP, then
$data = file_get_contents('http://host/path/file.php');
If you want to get the source code of the PHP file, then
$data = file_get_contents('path/file.php');
Remember that file_get_contents() will not work if your server has *allow_url_fopen* turned off.
//get the real path of the file in folder if necessary
$path = realpath("/path/to/myfilename.php");
//read the file
$lines = file($path,FILE_IGNORE_NEW_LINES);
Each line of the 'myfilename.php' will be stored as a string in the array '$lines'.
And then, you may use all string functions in php. More info about available string functions is available here: http://www.php.net/manual/en/ref.strings.php
I wish to create a list of XML files in a php file.
I need all my xml files to be included in my php file. I have this working see below.
<?php
header('Content-type: application/xml');
echo file_get_contents("exam1.xml");
when I try this it works well, now I try more than one.
<?php
header('Content-type: application/xml');
echo file_get_contents("exam1.xml");
echo file_get_contents("exam2.xml");
it dose not read any of the files.
I would try passing these files back in a JSON object.
Create an array of files and then json_encode() that array.
<?php
$f1 = file_get_contents("exam1.xml");
$f2 = file_get_contents("exam2.xml");
$response = array('files' => array($f1, $f2));
echo json_encode($response);
?>
Now you will have a simple json object that you can process in the javascript and place 1 or many xml file(s) whereever you want to on the page using straight forward javascript
So I have a page called create.php that creates another php file called "1". In this php file called "1". I was hoping to use
<?php echo $_SERVER['PHP_SELF'];?>
or
<?php $path = $_SERVER["SCRIPT_NAME"];echo $path;?>
To create a link that would take the number of the page and +1 it. When I do both of these functions instead of getting what I would think I would get, "1", I get "create", the page that it was created with. I'm quite dumbfounded by why this is happening, the code is most definitely on "1" and I even double checked to make sure create made a file and that I was on it so why does it think the current page is "create"?
Code being used
<?php
// start the output buffer
ob_start(); ?>
<?php echo $_SERVER['PHP_SELF'];?>
<?php
// open the cache file "cache/1" for writing
$fp = fopen("cache/1", 'w');
// save the contents of output buffer to the file
fwrite($fp, ob_get_contents());
fclose($fp);
ob_end_flush();
?>
You split the code in pieces and you probably have a wrong idea about what happens and what will be written in cache/1. Your code is the same as the following:
<?php
// start the output buffer
ob_start();
// echo the path of the current script
echo $_SERVER['PHP_SELF'];
// open the cache file "cache/1" for writing
$fp = fopen("cache/1", 'w');
// save the contents of output buffer to the file
fwrite($fp, ob_get_contents());
fclose($fp);
ob_end_flush();
I removed the closing PHP tag (?>) when it was followed by an open PHP tag (<?php).
Now it should be clear that, without output buffering, the script create.php display its own path relative to the document root. The output buffering captures the output and puts it into file cache/1.
You don't even need output buffering for this. You can simply remove all the calls to ob_* functions, remove the echo() line and use:
fwrite($fp, $_SERVER['PHP_SELF']);
It's clear that this is not your goal. You probably want to generate a PHP file that contains the following content:
<?php echo $_SERVER['PHP_SELF'];?>
This is as simple as it putting this text into a string and writing the string to the file:
<?php
$code = '<?php echo $_SERVER["PHP_SELF"];?>';
$fp = fopen("cache/1", 'w');
fwrite($fp, $code);
fclose($fp);
You can even use the PHP function file_put_contents() and all the code you posted in the question becomes:
file_put_contents('cache/1', '<?php echo $_SERVER["PHP_SELF"];?>');
If you need to put a bigger block of PHP code in the generated file then you can use the nowdoc string syntax:
$code = <<<'END_CODE'
<?php
// A lot of code here
// on multiple lines
// It is not parsed for variables and it arrives as is
// into the $code variable
$path = $_SERVER['PHP_SELF'];
echo('The path of this file is: '.$path."\n");
$newPath = dirname($path).'/'.(1+(int)basename($path));
echo('The path of next file is: '.$newPath."\n");
// That's all; there is no need for the PHP closing tag
END_CODE;
// Now, the lines 2-11 from the code above are stored verbatim in variable $code
// Put them in a file
file_put_contents('cache/1', $code);
I have two files reader.php and somesource.php. Both in the same folder.
somesource.php content inside the php tag.
echo "hello World";
reader.php contents
$fp = fopen('somesource.php','r') or die($php_errormsg);
$string = fread($fp,filesize('somesource.php'));
echo $string."<br>";
I am expecting to output
echo "hello World";
But I am seeing a blank page. I even tried. curl and file_get_contents. Both with the same output. If I write anything outside the php tag wil be echoed as normal. anything inside the php tag is skipped.
Please Help
use
echo htmlspecialchars($string)."<br>";
Depending on your server setup it may not read it without <?php echo $stuff; ?> in there. <? aka short tags can break a lot of stuff in my experience.
How to read a .php file using php
Let's say you have two files a.php and b.php on same folder.
Code on the file b.php
<?php
echo "hi";
?>
and code on a.php
<?php
$data = file_get_contents('b.php');
echo $data;
You access a.php on browser.
What do you see? A blank page.
Please check the page source now. It is there.
But not showing in browser as <?php is not a valid html tag. So browser can not render it properly to show as output.
<?php
$data = htmlentities(file_get_contents('b.php'));
echo $data;
Now you can see the output in browser.
If you want to get the content generated by PHP, then
$data = file_get_contents('http://host/path/file.php');
If you want to get the source code of the PHP file, then
$data = file_get_contents('path/file.php');
Remember that file_get_contents() will not work if your server has *allow_url_fopen* turned off.
//get the real path of the file in folder if necessary
$path = realpath("/path/to/myfilename.php");
//read the file
$lines = file($path,FILE_IGNORE_NEW_LINES);
Each line of the 'myfilename.php' will be stored as a string in the array '$lines'.
And then, you may use all string functions in php. More info about available string functions is available here: http://www.php.net/manual/en/ref.strings.php