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.
Related
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
I'm trying to receive a webhook from hipmob (live chat app) that should be triggered at a chat event.
Documentation: https://www.hipmob.com/documentation/chat-events.html
I've been following How to catch the HTTP POST request sent by a Shopify Webhook , and i have to say that i am completly new to this. I tried the solution from the first answer but with no success.
this is what i did:
<?php
$webhookContent = "";
$webhook = fopen('php://input' , 'rb');
while (!feof($webhook)) {
$webhookContent .= fread($webhook, 4096);
}
fclose($webhook);
$file = 'webhook.txt';
// Open the file to get existing content
$current = file_get_contents($file);
// Append a new person to the file
// Write the contents back to the file
file_put_contents($file, $current);
error_log($webhookContent);
?>
but with no success
I was able to solution it with:
<?php
$entityBody = file_get_contents('php://input');
$file = 'webhook.txt';
// Open the file to get existing content
$current = file_get_contents($file);
// Append a new person to the file
// Write the contents back to the file
file_put_contents($file, $entityBody);
error_log($webhookContent);
?>
This is quite a long-winded question as im completely lost!
The concept: User inputs a text file they wish to write to, upon submitting they are sent to a page where users can create shapes and submit them to the text file, this data is then used to work out the shapes area, colour that was selected etc...
Issue is how do i write to a text file that is in the session?
This is what i have on the home page:
<?php
// This line starts the session
session_start();
//The below calls the file
$txtFile = $_POST['submittedTxtFile'];
$_SESSION['submittedTxtFile']= $txtFile;
$file = fopen($txtFile, "r") or exit("That file does not exist");
include_once 'classShapeCollection.php';
//Creates the shapecollection
$shapes = new ShapeCollection();
//These lines get the called file, unserialize the $shapes and serialize them again before entering them into the session.
$buffer = fgets($file);
//Checking if there are any contents in the file
if($buffer)
{
$shapes = unserialize($buffer); //unserialize takes Text and turns it into an object
$_SESSION['serial']= serialize($shapes); //Serialize takes the objects and converts them into Text
}
else //if there is nothing in the file, the session serialises the new ShapeCollection
{
$_SESSION['serial']= serialize($shapes);
}
// Closes the called file
fclose($file);
?>
Opening the file as "r" means read only you should open it as write
fopen($txtFile, 'r+')
Or replace 'r+' with 'w+' if you want file to be truncated when opened
After closing the file handler, use file_put_contents() function to update the file. Like this:
fclose($file);
file_put_contents($txtfile, $_SESSION['serial']);
Make sure the file is writable.
Give this a try.
The following will write to a file called TEST.txt taken from the $write_session = "TEST"; session variable.
Base yourself on it, am sure you will get it to work the way you want it to, but that's basically how it will work.
<?php
session_start();
$_POST['submittedTxtFile'] = "file.txt"; // generic filename
$txtFile = $_POST['submittedTxtFile'];
$_SESSION['submittedTxtFile']= $txtFile;
$write_session = "TEST";
$_SESSION['write_session_write'] = $write_session;
$file = fopen($txtFile, "r") or exit("That file does not exist");
echo $_SESSION['submittedTxtFile'];
$file2 = $_SESSION['write_session_write'] . ".txt";
file_put_contents($file2, $write_session);
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
Ok I almost have it working but am having trouble escaping the html to preserve the link I'm missing something here .. I'm sure it's simple
if($_POST['formSubmit'] == "Submit")
$varUserName = $_POST['username'];
$varPW = $_POST['PW'];
$varEmail = $_POST['email'];
{
$fs = fopen("testcsv.csv","a");
fputcsv($fs, array($varUserName,$varPW,$varEmail,"admin","title",",category","some text here site.com",));
fclose($fs);
exit;
}
?>
I am not sure what you're having trouble with. Are you familiar with fopen();? Are you familiar with CSV?
You will need to open the file for appending and append to your file using fwrite(). Once done, close the file with fclose().
$fp = fopen("./filename", 'a'); //Open file for append
//fwrite($fp, $row1.",".$row2); //Append row,row to file
fputcsv($fp, array($name,$password,"http://$name.whatever.com")); //#Optimist
fclose($fp); //Close the file to free memory.