PDA

View Full Version : Resolved Inverse a



bluewalrus
12-05-2008, 06:30 AM
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.


$File = "questions.txt";
$Handle = fopen($File, 'a');
$Data = "<p>From:" . $visitor . "<br />Submitted on:" . $todayis . "<br />" . $notes . "</p><hr />";
fwrite($Handle, $Data);
fclose($Handle);

techietim
12-05-2008, 11:09 AM
Instead of a, try using r+.

bluewalrus
12-05-2008, 04:35 PM
That overwrites whatever is at the start

gpigate
12-05-2008, 04:42 PM
possibly try traversing an array of the file, adding elements prior to the start, then rewriting it?

bluewalrus
12-05-2008, 04:50 PM
Don't really get what you mean?

gpigate
12-05-2008, 05:00 PM
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.

bluewalrus
12-05-2008, 06:06 PM
hmmm I've never used arrays in php and haven't used an arrays in general in a while. You know of any tutorials?

gpigate
12-05-2008, 06:14 PM
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/5100-22_11-5077722.html

bluewalrus
12-05-2008, 06:41 PM
Ahh I've figured it out thanks.

For anyone else that is searching....


$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);

gpigate
12-05-2008, 06:43 PM
but reverse traversing an array and launching a lunar lander would have been much more fun and rewarding :)

glad you figured it out.

Twey
12-06-2008, 02:06 AM
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:
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);
}

bluewalrus
12-06-2008, 06:34 AM
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...


$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);
}?>

Twey
12-06-2008, 07:00 AM
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.

bluewalrus
12-06-2008, 09:02 PM
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.

Twey
12-07-2008, 06:25 AM
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.