Tutorial :Editing files in PHP



Question:

Is there a way to append or remove a line of text from a file using PHP.

I am in the process of writing a hosting control panel for my specific web hosting stack and would like to be able to make changes to the files with minimal requirements to touch the file system, and as such would like not to have to rewrite the whole file to add or remove a configuration option.


Solution:1

Working with files in PHP is full of pitfalls, particularly wrt concurrency and locking. The fact that you are writing config files implies also that you are exposing tasks normally only available to a user with root privileges over the web. You did not mention these things in your post, but your question is trivial in comparison to addressing these issues.

Regarding your question - although its fairly trivial to implement what you propose (e.g. by exec'ing sed - although you did not did say what OS this is running on) I'd recommend creating a copy of the original file in some other representation - obvious candidates would be a database - where you can apply sequence numbers to the lines and easily create a gap to populate, or a PHP SplDoublyLinkedList stored in the session. Then once the user has effected as many changes as they require, regenerate the file in a single operation from the working representation.

Note that, ultimately, regardless of how you implement the solution the solution will rewrite the entire file - its just a question of how much of this process is exposed within your code.

Bear in mind, that what you are doing is just the same as most php web scripts - except while they manipulate and reqrite HTML, you're doing it with a different file type - so you might want to look at how other PHP templating systems are written, and consider whether you can create a template for your config files.

HTH

C.


Solution:2

There is no way to remove a line from a file without first parsing the file into lines and then writing it out again.

You can append to a file by using fopen with the $mode set to 'a'

$fp = fopen('myfile', 'a');  


Solution:3

For appending, you should use fopen with the $mode of a.

See this please, on how to delete a line from the file.


Solution:4

Yes, you can open files in append mode:

$fh = fopen('testFile.txt', 'a');  

If you now write to the file, the new content gets appended.

See fopen and from this documentation:

'a': Open for writing only; place the file pointer at the end of the file. If the file does not exist, attempt to create it.

Removing the last line is not possible though.


Solution:5

If you need to change something in the middle of the file, you have to read it, parse it and save it back. Othwerwise you can only append something to the end of it.

You should not be concerned about the cost of this operation though, as it's a configuration file that won't likely be changed a hundred times every second, it should take negligible time.

If you want more flexibility in access/update/delete I think you should consider moving your configuration file to a database table.


Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »