How to open and edit a file on the server

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Joseph Cobham
    New Member
    • Jan 2012
    • 23

    How to open and edit a file on the server

    Hello all

    I would love to know if any body has a knowledge of opening a file like a html or a css file on the server and edit the file, after editing, perform a save on the file.

    Example like in joomla where one can open the html pages in a textbox and edit the file and save the file again.

    I would love to know how this could be done in php

    Any help will realy be appreciated.
  • Dormilich
    Recognized Expert Expert
    • Aug 2008
    • 8694

    #2
    PHP can open and save a file (file_get_conten ts(), file_put_conten ts()) but editing you have to do yourself. and since PHP is not an editor, you have to pass the data to an application that can handle the editing.

    Comment

    • Joseph Cobham
      New Member
      • Jan 2012
      • 23

      #3
      Originally posted by Dormilich
      PHP can open and save a file (file_get_conten ts(), file_put_conten ts()) but editing you have to do yourself. and since PHP is not an editor, you have to pass the data to an application that can handle the editing.
      Thank you very much for the snippet but how ever i am a little confused on how to go about this.

      Example i have a page called test.php on the same diretory as my current php page, i would like to output it in a texteditor and when i edit the outputed content and submit the form how do i write my php code to save the new file using the same mane as test.php in the same directory. thank you very much.

      Comment

      • Dormilich
        Recognized Expert Expert
        • Aug 2008
        • 8694

        #4
        that#s quite simple. when you send the edited content back to PHP, you get the sent string via GET/POST and (after verifying that the content is trustworthy) you can save it to disk. you can send the filename with this form as well so you don't have to lock your application.

        personally, I recommend to allow this only for logged in users, otherwise everyone could edit files on your server.

        Comment

        • Joseph Cobham
          New Member
          • Jan 2012
          • 23

          #5
          okay i tried file_get_conten t its only displaying the body part of the html code. I would like it to show the source code too including the scripts and head tags. heres my sample php code

          Code:
          <?php
          $t = file_get_contents('mypage.html');
          ?>
          
          <form>
          <textarea><?php echo html_entity_decode($t); ?></textarea>
          <input type="submit" Value="submit" />
          I would like the whole page content including the doctype,html,he ad,title and scripts tags to display in the textarea.

          Thank you.
          I appreciate it

          Comment

          • Dormilich
            Recognized Expert Expert
            • Aug 2008
            • 8694

            #6
            what does var_dump($t) give you?

            Comment

            • Joseph Cobham
              New Member
              • Jan 2012
              • 23

              #7
              Originally posted by Dormilich
              what does var_dump($t) give you?
              Hey thank you soooooooooooooo oooooooooo much i found that very helpful

              I appreciate it.

              Comment

              Working...