I have some other functions and a html page call after this simple function, but they dont run after this.
function page($name){
$content =<<<eol
<?php
PAGE CONTENT
?>
eol;
$file = "./search/$name.php";
$open = fopen($file, "w");
fwrite($open, $content);
fclose($open);
}
function works itself, but causes exiting code.
<?php
function page($name){
$content = '<?php PAGE CONTENT ?>';
$file = "./search/$name.php";
$open = fopen($file, "w");
fwrite($open, $content);
fclose($open);
}
page('file1.txt');
?>
Just see if that works, It could be something to do with your HEREDOC syntax (maybe the space before the eol;).
I disagree with Chief17
The only right thing is writing to file.
This code
<?php
PAGE CONTENT
?>
what supposed to do?
Did you mix up ???
may be it should be like this
?>
PAGE CONTENT
<?php
editing after your edit
You break the php like this why you need this anyway?
Related
I'm creating a html template with notification under nav bar , and admin can change that notification from the system the text of notification bar will be from notetxt file from the same location path where index.html is located i ave tried
<?php
foreach (glob("note.txt") as $filename) {
readfile($filename);
}
?>
and many other way but nothing happens it still stay blank
You are not echoing out the content of the textfile.
do it like this:
$myFile = "note.txt";
$fh = fopen($myFile, 'r');
$theData = fread($fh, filesize($myFile));
fclose($fh);
echo $theData;
This will output your content of the file.
i'm using this code in pure html file
You can't use PHP functions in plain HTML file. MUST be written in a PHP file.
You have now in your code:
<span>
<!--?php
foreach (glob("note.txt") as $filename) {
$fileArr = file_get_contents($filename);
}
?-->
</span>
Try with the examples above in a proper PHP file... then must work.
you can use file_get_contents function,
try something like this :
<?php
foreach (glob("note.txt") as $filename) {
$fileArr = file_get_contents($filename);
}
?>
It's very simple use file_get_contents();
<?= file_exists('note.txt') ? file_get_contents("note.txt") : "file doesn't exists"; ?>
That is all what you need. file_get_contents() get the content of file and returns it. I've also checked if file exists because it may be your problem. Also make sure you have proper rights to read the file(CHMOD) and file is not empty.
I'm using this code now to display a text file on a php/html page.
<?php
foreach (glob("example.txt") as $filename) {
echo nl2br(file_get_contents($filename));
echo "<br></br>";
}
?>
I'm looking for a way to display the example.txt file from another server with URI.
Something like this: http://address.com/dir/example.txt
Is there a simple way to do this?
(I would use an iframe but it's not possible to style the text without Java or JQuery).
You could just use
file_get_contents('http://address.com/dir/example.txt');
You code is totally wrong
foreach (glob("example.txt") as $filename) {
^------------------------- searching for a file
They can only one example.txt file in a folder at a time except you want to get all text files should should be like this in the first place
foreach (glob("*.txt") as $filename) {
If that is not the case the code would work for both remote and local file
error_reporting(E_ALL);
ini_set("display_errors", "on");
$fileName = "example.txt" ; // or http://oursite.com/example.txt
echo nl2br(file_get_contents($fileName));
echo "<br></br>";
You will have to use CURL to fetch the content of the file first and then display it.
Another option is to use iframes and set the target of iframe to the desired text file.
Yet another option is to use ajax to fetch the content from client end as suggested in comment.
Check fopen() / fread() and your available transport wrappers.
For normal length text file you can use:
<?PHP
$file = fopen("http://www.xyz.com/textfile.txt", "rb");
$output = fread($file, 8192);
fclose($file);
echo($output);
echo "<br>";
?>
For longer files:
<?PHP
$file = fopen("http://www.xyz.com/textfile.txt", "rb");
$output = '';
while (!feof($file)) {
$output .= fread($file, 8192);
}
fclose($file);
echo($output);
echo "<br>";
?>
It will prevent packet exceed issues in longer files by concatenating the file together in several groupings using while loop
My friend and I have a little spare time home page together. He's not a programmer, and in order for him to be able to change some text on the front page, I created a php-script that
1) Reads data from file "tester.txt" (this is the text that should go on the front page)
2) Prints this text to a textarea, where you can edit the text and submit it again
3) Writes the edited text to the same file, "tester.txt"
The two functions Read(); and Write(); look like this
function Read() {
$file = "tester.txt";
$fp = fopen($file, "r");
while(!feof($fp)) {
$data = fgets($fp, filesize($file));
echo "$data <br>";
}
fclose($fp);
}
function Write() {
$file = "tester.txt";
$fp = fopen($file, "w");
$data = $_POST["tekst"];
fwrite($fp, $data);
fclose($fp);
}
The only problem I have is that when the text is printed to a text area the line returns are written as <br> - and I don't really want it to do that, because when you edit some of the code and rewrites it, another layer of <br>'s appear. Here's a screenshot to illustrate:
Is there any workaround to this?
Thanks!
If you need the rest of the code, here it is:
<html>
<head>
<title>Updater</title>
</head>
<body>
<?php
function Read() {
$file = "tester.txt";
$fp = fopen($file, "r");
while(!feof($fp)) {
$data = fgets($fp, filesize($file));
echo "$data <br>";
}
fclose($fp);
}
function Write() {
$file = "tester.txt";
$fp = fopen($file, "w");
$data = $_POST["tekst"];
fwrite($fp, $data);
fclose($fp);
}
?>
<?php
if ($_POST["submit_check"]){
Write();
};
?>
<form action="<?php echo $_SERVER['PHP_SELF'] ?>" method="post">
<textarea width="400px" height="400px" name="tekst"><?php Read(); ?></textarea><br>
<input type="submit" name="submit" value="Update text">
<input type="hidden" name="submit_check" value="1">
</form>
<?php
if ($_POST["submit_check"]){
echo 'Text updated';
};
?>
</body>
</html>
This is simpler than you think. You shouldn't be outputting the <br> tags as the textarea already contains the entered newline characters (\r\n or \n). You don't have to read the file like that, if you read it this way, you never have to worry about the character contents.
Change:
$fp = fopen($file, "r");
while(!feof($fp)) {
$data = fgets($fp, filesize($file));
echo "$data <br>";
}
fclose($fp);
to:
echo file_get_contents( $file);
Problem solved.
This is happening because while writing the contents to the text area, you're putting a <br> at the end of each line. But in a textarea line breaks are noted by "\n". When you're saving your existing text, the next time the line breaks are replaced with more <br>.
While printing out the content on a public page, keep the . But in the editing page, remove the br.
Here's what I would have done with the PHP code:
<?php
define("FILE_NAME", "tester.txt");
function Read()
{
echo #file_get_contents(FILE_NAME);
}
;
function Write()
{
$data = $_POST["tekst"];
#file_put_contents(FILE_NAME, $data);
}
?>
<?php
if ($_POST["submit_check"])
{
Write();
}
?>
You use echo "$data <br>"; - just make that echo $data; ?
At a guess, this is hosted on a *nix machine, and he is using a Windows machine to do the editing?
If this is the case, changing your write function to this should solve the problem:
function Write(){
$file = "tester.txt";
$fp = fopen($file, "w");
$data = str_replace(array("\r\n","\r"),"\n",$_POST["tekst"]);
fwrite($fp, $data);
fclose($fp);
};
You don't need the <br>'s. Depending how your <textarea> is configured, by default lines are hard wrapped using \n's. These are preserved when you save the file, therefore you don't need to add your own line breaks.
Use regex to replace the break tag with a newline character
preg_replace('#<br\s*/?>#i', "\n", $data);
You can find a more detailed explanation answered here
I'm struggling trying to read a php file inside a php and do some manipulation..after that have the content as a string, but when I try to output that with echo or print all the php tags are literally included on the file.
so here is my code:
function compilePage($page,$path){
$contents = array();
$menu = getMenuFor($page);
$file = file_get_contents($path);
array_push($contents,$menu);
array_push($contents,$file);
return implode("\n",$contents);
}
and this will return a string like
<div id="content>
<h2>Here is my title</h2>
<p><? echo "my body text"; ?></p>
</div>
but this will print exactly the content above not compiling the php on it.
So, how can I render this "compilePage" making sure it returns a compiled php result and not just a plain text?
Thanks in advance
function compilePage($page, $path) {
$contents = getMenuFor($page);
ob_start();
include $path;
$contents .= "\n".ob_get_clean();
return $contents;
}
To evaluate PHP code in a string you use the eval function, but this comes highly unadvised. If you have a file containing PHP code, you can evaluate it with include, include_once, require, or require_once depending on your need. To capture the output of an included file - or required, or whichever method - you need to enable output buffering.
You can use output buffering for this, and include the file normally:
function compilePage($page,$path){
$contents = array();
$menu = getMenuFor($page);
ob_start();
include $path;
$file = ob_get_contents();
ob_end_clean();
array_push($contents,$menu);
array_push($contents,$file);
return implode("\n",$contents);
}
The include() call will include the PHP file normally, and <?php blocks will be parsed and executed. Any output will be captured by the buffer created with ob_start(), and you can get it later with the other ob_* functions.
You need to use include() so it will execute. You can couple this with output buffering to get the return in a string.
function compilePage($page,$path){
$contents = array();
$menu = getMenuFor($page);
//output buffer
ob_start();
include($path);
$file = ob_get_contents();
ob_end_clean();
array_push($contents,$menu);
array_push($contents,$file);
return implode("\n",$contents);
}
I am trying to display the contents of a .cpp file in php. I am loading it using fread when I print it out it comes out formatted incorrectly. How can I keep the format without escaping each character?
Assuming you want to look at it in a web browser:
<pre>
<code>
<?php echo htmlspecialchars(file_get_contents($file)); ?>
</code>
</pre>
print it out between the HTML <pre> & <code> tags.
<?php
echo "<pre><code>";
$filename = "./test.cpp";
$handle = fopen($filename, "r");
if ($handle) {
while (!feof($handle)) {
$buffer = fgets($handle, 4096); // assuming max line len is 4096.
echo htmlspecialchars($buffer);
}
fclose($handle);
}
echo "</code></pre>";
?>
We need htmlspecialchars function to print it out correctly.