Find the answer to your Linux question:
Results 1 to 2 of 2
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1

    Editing txt files with PHP

    I have a PHP script which, when run, brings a txt file into a text area in a browser, allows the browser user to edit the contents and then when the user clicks on a "save" button, saves the changed contents back to the original file on disk.

    It relies upon the PHP reading the WHOLE file, displaying the WHOLE file and writing back the WHOLE file. That can be cumbersome with big files, and makes writing guidance to GUI users complex and fallable.

    I'd like to be able to read the file in to memory with PHP, have php and something like grep identify a "trigger" word or string, display the next "n" characters after the trigger in the GUI, let the user edit just the bit of the file shown, then write the browser-made changes back to the file in memory, and finally write the whole file back over the original file.

    I have not the faintest idea how to do this....can anyone point me in the right direction; code snippets would be useful as well

    Thanks for any help


  2. #2
    Super Moderator Roxoff's Avatar
    Join Date
    Aug 2005
    Nottingham, England
    Well, you can already load and save the file across the session of edits. All you need to do is to find the snippets in the file you want and present them for edit?

    You can find bits in the already-loaded file contents with features like the php strpos function which will find substrings within a larger string. You can then use the php substr function to grab the bits out of the string you're passing to the client to edit. If you store the position of the edited string in the session somewhere, then the text can be replaced when it comes back in from the client.
    Linux user #126863 - see

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts