Updating text files using php

I use the following code to create a rudimentary text editor. You could easily add a parameter to specify a file to edit; I have not done so to avoid the potential security headaches.

There are still obvious security holes here, but for most applications it should be reasonably safe if implemented for brief periods in a counterintuitive spot.

After we finished writing, we closed the file using the Now that "newfile.txt" contains some data we can show what happens when we open an existing file for writing.

All the existing data will be ERASED and we start with an empty file.

More information about using this function without upgrading your version of PHP can be found on the below link: It's worth noting that you must make sure to use the correct path when working with this function.

I was using it to help with logging in an error handler and sometimes it would work - while other times it wouldn't.

I'm updating a function that was posted, as it would fail if there was no directory.

It also returns the final value so you can determine if the actual file was written.

/usr/bin/php Here is a stupid pitfall I just fell into.* * @param int $flags same flags used for file_put_contents.* more info: @return bool TRUE file created succesfully FALSE failed to create file.Note that opening a file returns a file handle - you must read from that if you want to access the existing data.Also note that if you're writing back to the same file that you're reading from, you much complete the read before opening the file for write!You need to open your input file with a read attribute.

You must have an account to comment. Please register or login here!