-
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.