sunnysidesounds Posted July 20, 2009 Share Posted July 20, 2009 Hello fellow forum goers, I am new to this forum so please forgive any newbie mistake that I make. I am fairly new to PHP in some regard as well, so if I don't explain things properly again sorry in advance. I am currently experimenting with a PHP file directory. Where you view files in your browser from a directory (i.e. folder) on the server. The issue that I am having is with creating a file editor, so that I can edit these files directly in my browser. Kind of like how some CMS's do like Drupal and Typo3. Here is the situation: I have a list of files and it is laid out in a table with the usual stuff like file name, size, date. I also have a edit button. Where when I click the button it posts the contents of the file inside a textrea. Here is the edit button code, pretty basic. <form method='post' action='edit.php'><input type='hidden' name='fileToEdit' value='".$fName."'><input type='submit' value='Edit'></form> Again when I click the button, it bring me to a page with the file contents are within a text area. Here is the PHP code for this: <?php //Name of File: $loadcontent = $_POST['fileToEdit']; //When the textarea is submitted if($_POST['save_file']) { $savecontent = stripslashes($_POST['savecontent']); $fp = @fopen($loadcontent, "w"); if ($fp) { fwrite($fp, $savecontent); fclose($fp); print '<a href='.$_SERVER[php_SELF].'>Refresh</a>'; print "<html><head><META http-equiv=\"refresh\" content=\"0;URL=$_SERVER[php_SELF]\"></head><body>"; } } //Loads the file contents $fp = @fopen($loadcontent, "r"); $loadcontent = fread($fp, filesize($loadcontent)); $lines = explode("\n", $loadcontent); $count = count($lines); $loadcontent = htmlspecialchars($loadcontent); fclose($fp); for ($a = 1; $a < $count+1; $a++) { $line .= "$a\n"; } ?> <form method=post action="<?=$_SERVER['PHP_SELF']?>"> <input type="submit" name="save_file" value="Save"> <table width="100%" valign="top" border="0" cellspacing="1" cellpadding="1"> <tr> <td width="3%" align="right" valign="top"><pre style="text-align: right; padding: 4px; overflow: auto; border: 0px groove; font-size: 12px" name="lines" cols="4" rows="<?=$count+3;?>"><?=$line;?></pre></td> <td width="97%" align="left" valign="top"> <textarea style="text-align: left; padding: 0px; overflow: auto; border: 3px groove; font-size: 12px" name="savecontent" cols="150" rows="<?=$count;?>" wrap="OFF"><?=$loadcontent?> </textarea></td> </tr> </table> <br> <input type="submit" name="save_file" value="Save"> </form> The issue that I am having is that when I edit the file within the textarea and click "Save". It doesn't save what I edited to the file. I believe the issue is that the textarea form doesn't know where to post the data. It doesn't know where the file is to save the information. (Is this correct??) Anyway, I have seen lots of example of this similar code, but for the $loadcontent variable it is just using a static file. For example: $loadcontent = 'test.txt'; instead of using the $loadcontent = $_POST['fileToEdit']; Anyway, any suggestion would be greatly appreciated. Please note that I know this method of editing files online could be a security risk. So I have taken this into account. Oh, and sorry for the long posts. I just wanted to cover all my bases. Thanks in Advance, Jason Link to comment https://forums.phpfreaks.com/topic/166640-issues-creating-a-file-editor-with-freadfwrite/ Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.