Jump to content

Loading file contents in to window on submit(post)


dmikester1

Recommended Posts

I have a directory/file listing on my site.  I can click "edit" next to each of the files to pop up  a basic file editor which loads in the text of the file using file_get_contents($newFile);  Now, I have a save button at the bottom and when I click it, it saves the edited text to the current file being worked on.  Then it pops up a message at the bottom saying "file saved" with javascript.  The problem is, when I post, it reloads the window loading in the unedited file contents.  Here is my form code:

<form method="post" action="<?php echo $_SERVER['PHP_SELF']."?dir=".$_GET['dir']."&file=".$_GET['file'];?>">
<div><textarea rows="25" cols="70" style="border: 1px solid #666666;" id="updatedfile" name="updatedfile">
<?php
$filedir = $_GET['dir'];

if($filedir == "") {
  $filedir = ".";
}
if(is_readable($filedir))  //check that the get "value" is readable
{
$newFile = $_GET['file'];


  if ($newFile == "ht") {
  $newFile = ".htaccess";
$fileText = file_get_contents($newFile);
echo $fileText;
?></textarea><br /></div>
<div class = "center">File will be saved as: <strong>.htaccess</strong>
<?php
  }
  else if($newFile == "") {
  ?></textarea></div>
  <div class = "center">Name of file: <input type="text" name="filename" id="filename">
  <?php
  }
  else {
  $fileText = file_get_contents($newFile);
echo $fileText;
?></textarea><br /></div>
<div class = "center">File will be saved as: <strong><?php echo $newFile ?></strong>

  <?php
}

?>
<br /><br /></div>
<div id="hidden"></div>

<div><input type="submit" name="save" id="save" value="Save Changes" 
onclick="return checkIfEmpty('filename')"></div>
<?php
  if (isset($_POST['save'])) {  //if the "save" button was clicked
    if($newFile == "") {
    $file = $_POST['filename'];
	}
	else {
	$file = $newFile;
	}

    $file = $cwd.$file;
    $file2save = fopen($file, "w+");
    if (is_writable($file))
    {
      $data_to_save = $_POST["updatedfile"];
      $data_to_save = eregi_replace("<END-TA-DO-NOT-EDIT>", "</textarea>", $data_to_save);
        
      if (fwrite($file2save,$data_to_save))
      {
      ?><script type="text/javascript">
        document.getElementById('updatedfile').focus();
</script> <?php
        echo "file saved!";
        //Close file
        fclose($file2save);
      }else {
        //If write fails show failure page
        echo "file not saved!";
        //Close file
        fclose($file2save);
      }
    }else {
      echo "not writable!";
    }
}
}
?>
</form>

 

Thanks

Mike

 

Link to comment
Share on other sites

Im not 100% sure i've fully understood but what i think is happening is:

 

1.) You edit the file

2.) You click save

3.) You get the correct message saying it has saved AND if you view the file again it has indeed been saved successfully

4.) BUT along with the saved message, you see the old code

 

If thats about right, then it's because you're doing the saving at the bottom of your script, after the contents is grabbed from the file and displayed. You need to be doing the form processing at the top.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.