+ Reply to Thread
Results 1 to 4 of 4

Thread: Edit a file with a form

  1. #1
    driveflexfuel is offline x10 Sophmore driveflexfuel is an unknown quantity at this point
    Join Date
    Jul 2008
    Posts
    159

    Edit a file with a form

    I have a file that i wish to edit using a form. I only want to load and edit a specific portion of the page. If anyone can help with this I thank you ahead of time.

    This is what the code looks like
    Code:
    coding here
    
    <!--Load from here-->
    This is the editable portion of the page i wish to load into the form
    <!--Load to here-->
    
    more coding here

  2. #2
    VPmase's Avatar
    VPmase is offline x10 Elder VPmase is an unknown quantity at this point
    Join Date
    Nov 2007
    Location
    Dixon, IL, USA
    Posts
    914

    Re: Edit a file with a form

    One way you can do it is by using multiple files and only loading the editable file. That would probably be the easiest and fastest way.

  3. #3
    garrettroyce's Avatar
    garrettroyce is offline Generally Helpful Member garrettroyce is a glorious beacon of lightgarrettroyce is a glorious beacon of light
    Join Date
    Apr 2008
    Location
    IL, USA
    Posts
    3,746

    Re: Edit a file with a form

    Quote Originally Posted by VPmase View Post
    One way you can do it is by using multiple files and only loading the editable file. That would probably be the easiest and fastest way.
    This is a good way to do it. Also, when you edit a file, depending on how you open the file, it may only be accessible to one person at a time. So, if you plan on making a file editable by more than one user at once, the users may get an error if someone is already editing it. This also includes you, if you are trying to edit a file using the same file, as VPmase mentioned :P

    I usually save all writing until the very last possible moment to minimize the file locking problems. Also, using fclose() when you don't need a file handle and closing any open resources can help minimize this problem as well.
    gjr.gr - coming soon: secrets of OCD coding from a self taught tinkerer

  4. #4
    gomarc's Avatar
    gomarc is offline x10 Elder gomarc is an unknown quantity at this point
    Join Date
    Oct 2007
    Location
    USA
    Posts
    511

    Re: Edit a file with a form

    PHP Code:
    <?php
     
    //Variables to edit
    $filename "file2edit.txt"//File to edit
    $startmark"<!--Load from here-->";
    $endmark  "<!--Load to here-->";
     

    $source file_get_contents($filename)
      or exit(
    "</br>Problem reading <b>'".$filename."'</b> (check file name)");
      
    $fstart strpos($source$startmark
      or exit(
    "</br>Error: Start mark on file not found");
    $fstart $fstart strlen($startmark);  

    $fend strpos($source$endmark
      or exit(
    "</br>Error: End mark on file not found");  

    //Split the file
    $part1 substr($source0$fstart);
    $part2 substr($source$fstart , ($fend-$fstart));
    $part3 substr($source$fend , (strlen($source)-$fend));

    ?>

    <FORM METHOD="POST" ACTION="save_changes.php">

    <P>Edit your file <?=$filename?>:<br>

    <TEXTAREA NAME="part2" COLS=80 ROWS=15 WRAP=virtual>
    <?=$part2?>
    </TEXTAREA></P>
    <input type="hidden" name="fname" value="<?=$filename?>">
    <input type="hidden" name="part1" value="<?=$part1?>">
    <input type="hidden" name="part3" value="<?=$part3?>">

    <P><INPUT TYPE="submit" NAME="submit" VALUE="Save Changes"></P>

    </FORM>
    And the file save_changes.php is
    PHP Code:
    <?php

    IF (ISSET($_POST['fname']))
    {

      
    $Newfile $_POST['part1'] . $_POST['part2'] . $_POST['part3'];

      
    $file fopen($_POST['fname'],"w");
      
    fwrite($file,$Newfile);
      
    fclose($file);

      echo 
    "<br>Changes to file <b>".$_POST['fname']."</b> have been saved";

    }
    else
    {
      echo 
    "<br>Nothing done";
    }

    ?>

+ Reply to Thread

Similar Threads

  1. PHP writing to a file outside a directory
    By NightFox in forum Programming Help
    Replies: 3
    Last Post: 01-23-2009, 05:22 PM
  2. Problem with file copy script
    By DaveBC in forum Programming Help
    Replies: 5
    Last Post: 01-12-2009, 02:14 PM
  3. On dial-up, can't edit file.
    By StingReay in forum Free Hosting
    Replies: 1
    Last Post: 07-07-2008, 09:10 AM
  4. How I can edit my DNS Zone File ?
    By dualservers in forum Free Hosting
    Replies: 4
    Last Post: 06-12-2008, 12:37 AM
  5. [PHP]Simple Flat File Login Form
    By BrettFreeman in forum Tutorials
    Replies: 21
    Last Post: 10-15-2007, 11:33 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
x10hosting free hosting for the masses
dedicated servers