-
Inverse a
In the fopen($File, 'a'); function is there a way to have it write at the start of the file rather then the end; so newest things being added would appear first?
This is my current code to clarify a bit what I'm doing maybe. Maybe this won't help at all.
PHP Code:
$File = "questions.txt";
$Handle = fopen($File, 'a');
$Data = "<p>From:" . $visitor . "<br />Submitted on:" . $todayis . "<br />" . $notes . "</p><hr />";
fwrite($Handle, $Data);
fclose($Handle);
-
Instead of a, try using r+.
-
That overwrites whatever is at the start
-
possibly try traversing an array of the file, adding elements prior to the start, then rewriting it?
-
Don't really get what you mean?
-
i havent actually tried it so I can not give you a code example. but you should be able to load your file into an array, then sort the array in reverse order. add your line to the array which making it higher in element. then write the array back to the file.
you would probably need to crush the file when writing it back. ie deleting the content and rewriting it.
-
hmmm I've never used arrays in php and haven't used an arrays in general in a while. You know of any tutorials?
-
try googling it. you will have to sort through a bunch of invalid examples etc.
$fp = @fopen($filename, 'r');
if ($fp) {
$array = explode("\n", fread($fp, filesize($filename)));
}
that will load a file into an array.
while (list($key,$value) = each($array)) {
echo "$key : $value<br>";
this should show you if you have values in the array after reading the file
once you have that working... you could then use rsort function or something else to sort the array in reverse order. stick whatever you want in the array as the highest element and then write it back to the file.
the above code is not mine, something i found on google pretty quickly. i dont know if it works but at a glance seems syntactically ok
pretty good article.
http://articles.techrepublic.com.com...1-5077722.html
-
Ahh I've figured it out thanks.
For anyone else that is searching....
PHP Code:
$File = "textfilename.txt";
$Handle = fopen($File, "r+");
$Data = "WHATEVER YOU WANT TO BE INSERTED";
if ($Data <> "")
{
$existingText = file_get_contents($File);
fwrite($Handle, $Data . $existingText . "\n");
}
fclose($Handle);
-
but reverse traversing an array and launching a lunar lander would have been much more fun and rewarding :)
glad you figured it out.
-
That works, but is dangerous on large files — your script will quickly run out of memory and die. Rather, a better solution involves a temporary file:
Code:
define('BUFSIZ', 512); // Handle half a kilobyte at a time.
// fcopy(resource $src, resource $dest):
// copy the contents of $src to $dest.
// $src must be readable, and $dest writeable.
function fcopy($src, $dest) {
for (; !feof($src); $buf = fread($src, BUFSIZ))
fwrite($dest, $buf);
}
// file_prepend_contents(string $filename, string $data):
// prepend some data to a file.
function file_prepend_contents($filename, $data) {
if ($data === '') return;
// Open the provided filename, and a temporary file.
$f = fopen($filename, 'r');
$ft = tmpfile();
// Copy the contents of the file to the temporary file.
fcopy($f, $ft);
// Seek the temporary file (which is in r+ mode) to the beginning,
// and re-open the main file for writing, truncating it.
fclose($f);
fseek($ft, 0);
$f = fopen($filename, 'w');
// Write the data to the main file, followed by the original contents.
fwrite($f, $data);
fcopy($ft, $f);
// Close the handles.
fclose($f);
fclose($ft);
}
-
Hmmm I tried replacing my code with that but it didn't write to the text file. It brought up the success message but the data wasn't in the text file this is how i put it in...
PHP Code:
$File = "questions.txt";
$Handle = fopen($File, "r+");
$Data = "<p>From:" . $visitor . "<br />Submitted on:" . $todayis . "<br />" . $notes . "</p><hr />";
define('BUFSIZ', 512); // Handle half a kilobyte at a time.
// fcopy(resource $src, resource $dest):
// copy the contents of $src to $dest.
// $src must be readable, and $dest writeable.
function fcopy($src, $dest) {
for (; !feof($src); $buf = fread($src, BUFSIZ))
fwrite($dest, $buf);
}
// file_prepend_contents(string $filename, string $data):
// prepend some data to a file.
function file_prepend_contents($File, $data) {
if ($data === '') return;
// Open the provided filename, and a temporary file.
$f = fopen($File, 'r');
$ft = tmpfile();
// Copy the contents of the file to the temporary file.
fcopy($f, $ft);
// Seek the temporary file (which is in r+ mode) to the beginning,
// and re-open the main file for writing, truncating it.
fclose($f);
fseek($ft, 0);
$f = fopen($File, 'w');
// Write the data to the main file, followed by the original contents.
fwrite($f, $data);
fcopy($ft, $f);
// Close the handles.
fclose($f);
fclose($ft);
}?>
-
Are you familiar with the concept of a function? If not, you should probably go through a programming language tutorial.
In a nutshell: a function is a reusable block of code that you can call with your own variables as parameters. There is no need to substitute in your variable, but you must call the function — it won't do anything without being called.
By convention, variables do not start with a capital letter.
-
I had not heard of case sensitive variables. Is $File different from $file? I've used functions before but not with php. I haven't done a lot with php...
So I should leave my variables as they were declared, paste your original code in, as it was under my variables, and it should write to the test file?
Thanks for your help.
-
In PHP, $File is the same as $file is the same as $FILE (this being the meaning of 'case-insensitive'), but by convention it would be written $file.
It is a function: it won't do anything until you call it, passing in the appropriate values. Please, read a programming language tutorial.