Ok sorry if its a stupid question im a begginer.
Im making a small shoutbox just for practise.
It inserts the shout infos in a txt file.
My problem is that, it lists the text from top to bottom, and i would like to do this reversed.
if(isset($_POST['submit'])) {
$text = $_POST['text'];
if(!empty($text)) {
$text = $_POST['text'];
$name = $_POST['name'];
$time = date("H:i");
$content =
"<div class='text'><em>" . $time . "</em>
<span class='c11'><b>" . "<a href='userinfo_php_willbe_here.php' target='_blank'>" . htmlspecialchars($name) . "</a>" . ":</span></b>
" . htmlspecialchars($text) . "
</div>\n";
file_put_contents($file, $content, FILE_APPEND | LOCK_EX);
}
}
here is my code.
i was googleing around with not much luck maybe i wasnt looking hard enough.
could please someone give me a hint?
thank you
No way to do so with one function call. You need to read the content from your target file, prepend the data in php and rewrite the whole file (see file_get_contents).
$fileContent = file_get_contents($file);
$fileContent = $content . $fileContent;
file_put_contents($file, $fileContent, LOCK_EX);
You can also use the array_reverse like so:
// Data in file separated by new-line
$data = explode("\n",file_get_contents("filename.txt"));
foreach(array_reverse($data) as $value) {
echo $value."\n";
}
You can only prepend to a file by means of reading it and writing afterwards.
file_put_contents($file, $content . file_get_contents($file), LOCK_EX);
Related
How to log GET request parameters to text file? For example write.php?info=LOG_THIS. I want to append the ip address followed by ": LOG_THIS" on new line in text file. How to do that?
file_put_contents("your_file.txt", $_SERVER["REMOTE_ADDR"].
":" . $_GET["info"], FILE_APPEND | LOCK_EX);
Documentation: http://php.net/manual/en/function.file-put-contents.php
<?php
//get data from file
$content=file_get_contents("info.txt");
//add ip
$content .=$_SERVER["REMOTE_ADDR"];
//add get
$content .= $_GET["info"];
//write to file
file_put_contents("info.txt",$content);
?>
You can find more infos on http://php.net ...
//**********************START generate Log File**************************
<?php
$content=$_GET["info"];
wh_log("Log generate".$content);
function wh_log($msg)
{
$logfile = 'log_' . date('d-M-Y') . '.txt';
file_put_contents($logfile, $msg . "\n", FILE_APPEND);
}
?>
//END generate LOG FILE
I have some php code. I wish to unset topic and unlink $outputfile once I have finished printing it.
$file = "files/textfile.txt" ;
$processed = "processed.txt" ;
$outputfile = "files/outputfile.txt" ;
if(!empty($_POST)){
writetofile($file, $text, "w");
preproc($file);
execpython($processed);
$topic = getoutput($outputfile);
}
So, I am printing it somewhere at the bottom:
echo '<span style="color:red;">' . $topic . '</span>';
I am not sure how should I place unlink and unset because they seem to be deleting the outputfile in advance and I don't receive any output then. I need to delete it because it is being over-written and producing the same output sometimes.
You can delete the file after you read from it with getoutput(), and you can unset the variable after you're finished printing it.
$file = "files/textfile.txt" ;
$processed = "processed.txt" ;
$outputfile = "files/outputfile.txt" ;
if(!empty($_POST)){
writetofile($file, $text, "w");
preproc($file);
execpython($processed);
$topic = getoutput($outputfile);
unlink($outputfile);
}
echo '<span style="color:red;">' . $topic . '</span>';
unset($topic);
I have a txt file with lines in this format
this is a name|this is a type|this is a description
this is a name|this is a type|this is a description
this is a name|this is a type|this is a description
I need to access those lines and echo them like this:
<li type="this is a type" description="this is a description">this is a name</li>
I have no idea how to approach this.
Thanks in advance
Normally I wouldn't write code for you without you having provided an example of what you've tried, but in this case it's pretty basic.
Use PHP's file function to read a file into an array (line by line), then use explode to break that line up:
<?php
$contents = file('yourfile.txt');
foreach($contents as $eachline) {
list($name, $type, $description) = explode("|", $eachline);
echo '<li type="' . $type . '" description="' . $description . '">' . $name . '</li>';
}
?>
PHP manual: http://us2.php.net/manual/en/function.file.php
The first step is to read every line of the file.
How to read a file line by line in php
After that explode the string by the pipe symbol $out = explode("|", $string);
after that you have an array and you can access the values with $out[0]; for example.
This is easypeasy:
$parts = explode("|", $line);
$out = "<li type='$parts[1]' description='$parts[2]'>$parts[0]</li>"
This question already has answers here:
Need to write at beginning of file with PHP
(10 answers)
Closed 9 years ago.
I've got this PHP script:
<?php
if ('POST' === $_SERVER['REQUEST_METHOD'] && ($_POST['title'] != 'Title') && ($_POST['date'] != 'Date'))
{
$fileName = 'blog.txt';
$fp = fopen('blog.txt', 'a');
$savestring = PHP_EOL . "<h2><center><span>" . $_POST['title'] . "</span></center></h2>" . "<div class=fright><p><em>|<br><strong>| Posted:</strong><br>| " . $_POST['date'] . "<br>|</p></em></div></p></em>" . "<p><em>" . $_POST['paragraph'] . "</em></p>" . PHP_EOL . "<hr>";
fwrite($fp, $savestring);
fclose($fp);
header('Location: http://cod5showtime.url.ph/acp.html');
}
?>
It works perfectly but it has a slight problem. The text is added at the end of the file. Is there a way to make it add the $savestring at the beginning of the text file ? I'm using this for my blog and I just noticed this slight problem.
You need to use the correct writing mode:
$fp = fopen('blog.txt' 'c');
http://us1.php.net/manual/en/function.fopen.php
You can you use
$current = file_get_contents($file);
// Append a data to the file
$current .= "John Smith\n";
file_put_contents($file, $current, FILE_APPEND | LOCK_EX);
I am trying to handle a CSV file via php, and I have it working. But there is this one array that I need to change based on a set of conditions.
$file_handle = fopen($path, "r");
while (!feof($file_handle) ) {
$line_of_text = fgetcsv($file_handle, 100000);
if($currency == "US"){
$line_of_text[6] = str_replace ("if_you_find_this","change_to_this",$line_of_text[6]);
$line_of_text[6] = str_replace ("if_you_find_this","change_to_this",$line_of_text[6]);
} elseif($currency == "DE"){
$line_of_text[6] = str_replace ("if_you_find_this","change_to_this",$line_of_text[6]);
$line_of_text[6] = str_replace ("if_you_find_this","change_to_this",$line_of_text[6]);
}else {
echo "Something with currency handling went wrong. Please contact support.";
}
$data .= $line_of_text[0] . "," . $line_of_text[1] . "," . $line_of_text[2] . "," . $line_of_text[4] . "," . $line_of_text[6] . "," . $line_of_text[49] . "," . $line_of_text[51] . "\n";
}
fclose($file_handle);
$new_file_handle = fopen($path, "w");
fwrite($new_file_handle, $data);
It's not throwing any errors, but seems that the whole conditional block is being ignored. Help?
You're not reading from $file_handle anywhere. feof($file_handle) will never be true. As posted, this code should loop forever.
(BTW, using feof like this is generally not how you want to do it, for this reason among others. Better would be something like while (($line = however_you_read($file_handle)) !== FALSE).) Pretty much all the stream-reading functions return FALSE on any error, or on EOF.)
Check file is correctly opened or no.
Write that code in try catch, so if any error then you can find it.
I think you file doesn't contain anything or it doesn't open successfully.