doomdude Posted August 14, 2011 Share Posted August 14, 2011 Hey guys, not sure how this is possible. But I'm looking to edit contents of a .php file with a form from another .php file. Just wondering how this is possible? I believe its possible since sites like wordpress edit the config.php file while installing. I've looked for some examples but cannot seem to find any, does anyone have any links to an example or an example of there work editing a .php file from a .php form. Thanks Quote Link to comment Share on other sites More sharing options...
ZulfadlyAshBurn Posted August 14, 2011 Share Posted August 14, 2011 you may want to use fopen to open the file? Quote Link to comment Share on other sites More sharing options...
doomdude Posted August 14, 2011 Author Share Posted August 14, 2011 If I use fopen and fwrite, that will replace everything in the file and write just the submitted data? Or am I way off the mark. Quote Link to comment Share on other sites More sharing options...
ZulfadlyAshBurn Posted August 14, 2011 Share Posted August 14, 2011 you may want to try this. echo "<form action='edit.php' method='POST'><textarea name='editpage'>"; $filename = "index.php"; // replace to actual file name $handle = fopen($filename, "r"); $contents = fread($handle, filesize($filename)); echo $contents; fclose($handle); echo "</textarea><input type='submit' value='Submit'/></form>"; that will place everything in the file into a textarea which is in a form the will pass the data to edit.php Quote Link to comment Share on other sites More sharing options...
voip03 Posted August 14, 2011 Share Posted August 14, 2011 PHP - File Write: Appending Data The only thing that is different is that the file pointer is placed at the end of the file in append mode, so all data is added to the end of the file. $myFile = "testFile.txt"; $fh = fopen($myFile, 'a'); Quote Link to comment Share on other sites More sharing options...
doomdude Posted August 14, 2011 Author Share Posted August 14, 2011 you may want to try this. echo "<form action='edit.php' method='POST'><textarea name='editpage'>"; $filename = "index.php"; // replace to actual file name $handle = fopen($filename, "r"); $contents = fread($handle, filesize($filename)); echo $contents; fclose($handle); echo "</textarea><input type='submit' value='Submit'/></form>"; that will place everything in the file into a textarea which is in a form the will pass the data to edit.php I tried the code you said but it just prints the code of that page in the text box? Quote Link to comment Share on other sites More sharing options...
ZulfadlyAshBurn Posted August 14, 2011 Share Posted August 14, 2011 its works pretty well on my local server o.0 make sure that the apostrophes are correct? Quote Link to comment Share on other sites More sharing options...
doomdude Posted August 14, 2011 Author Share Posted August 14, 2011 Sorry I'm an idiot... didn't add php tags lol. It loads the file into the text area but pressing submit doesn't save the edit? Quote Link to comment Share on other sites More sharing options...
ZulfadlyAshBurn Posted August 14, 2011 Share Posted August 14, 2011 oh lol. do you even have a page which is edit.php? Quote Link to comment Share on other sites More sharing options...
doomdude Posted August 14, 2011 Author Share Posted August 14, 2011 Yeh, (well editer.php). Editer.php: <?php echo "<form action='editer.php' method='POST'><textarea name='editpage' rows='20' cols='50'>"; $filename = "includes/config.php"; // replace to actual file name $handle = fopen($filename, "r"); $contents = fread($handle, filesize($filename)); echo $contents; fclose($handle); echo "</textarea><input type='submit' value='Submit'/></form>"; ?> Displays: but submit just refreshes the page. Quote Link to comment Share on other sites More sharing options...
ZulfadlyAshBurn Posted August 14, 2011 Share Posted August 14, 2011 this is how you are gonna have it done. use this exact code. // check if user edited the page. if(isset($_REQUEST['editfile'])) { $editfile = $_REQUEST['editfile']; $filename = "r.php"; $handle = fopen($filename, "w+"); fwrite($handle, $editfile); fclose($handle); echo "Done Editing!"; } // if not show the form else { $self = $_SERVER['PHP_SELF']; echo "<form action='" .$self. "' method='post'/><textarea name='editfile' rows='20' cols='50'>"; $filename = "includes/config.php"; $handle = fopen($filename, "r"); $contents = fread($handle, filesize($filename)); echo $contents; fclose($handle); echo "</textarea><input type='submit' value='Submit'/></form>"; } Quote Link to comment Share on other sites More sharing options...
doomdude Posted August 14, 2011 Author Share Posted August 14, 2011 Hmmm That works to the point where it echos done editing, but the includes/config.php file has not got the changes saved. Quote Link to comment Share on other sites More sharing options...
ZulfadlyAshBurn Posted August 14, 2011 Share Posted August 14, 2011 opps. sorry. i forget to change the save point to includes/config.php // check if user edited the page. if(isset($_REQUEST['editfile'])) { $editfile = $_REQUEST['editfile']; $filename = "includes/config.php.php"; $handle = fopen($filename, "w+"); fwrite($handle, $editfile); fclose($handle); echo "Done Editing!"; } // if not show the form else { $self = $_SERVER['PHP_SELF']; echo "<form action='" .$self. "' method='post'/><textarea name='editfile' rows='20' cols='50'>"; $filename = "includes/config.php"; $handle = fopen($filename, "r"); $contents = fread($handle, filesize($filename)); echo $contents; fclose($handle); echo "</textarea><input type='submit' value='Submit'/></form>"; } it should work now Quote Link to comment Share on other sites More sharing options...
doomdude Posted August 14, 2011 Author Share Posted August 14, 2011 Nearly worked Thank you so much just had to take off the second .php here: $filename = "includes/config.php.php"; Is there a way to hide the bottom part? e.g not variables. Quote Link to comment Share on other sites More sharing options...
ZulfadlyAshBurn Posted August 14, 2011 Share Posted August 14, 2011 sorry, my bad. so you just want the user to edit the db part? Quote Link to comment Share on other sites More sharing options...
doomdude Posted August 14, 2011 Author Share Posted August 14, 2011 Yeh if possible how would I hide: $connection = mysql_connect($dbhost, $dbuser, $dbpass) or die ("Could not connect to server ... \n" . mysql_error ()); mysql_select_db($dbdata) or die ("Could not connect to database ... \n" . mysql_error ()); But show: $dbhost = 'localhost'; $dbuser = 'root'; $dbpass = ''; $dbdata = 'accounts'; But still save the whole lot in the config.php file lol (I know I'm a pain) Quote Link to comment Share on other sites More sharing options...
ZulfadlyAshBurn Posted August 14, 2011 Share Posted August 14, 2011 you may want to have the $dbhost = 'localhost'; $dbuser = 'root'; $dbpass = ''; $dbdata = 'accounts'; in another file named connDB.php then make it require from your config.php thus the config, connDB & editer page would look like this. config.php <?php require("connDB.php"); $connection = mysql_connect($dbhost, $dbuser, $dbpass) or die ("Could not connect to server ... \n" . mysql_error ()); mysql_select_db($dbdata) or die ("Could not connect to database ... \n" . mysql_error ()); ?> connDB.php <?php $dbhost = 'localhost'; $dbuser = 'root'; $dbpass = ''; $dbdata = 'accounts'; ?> then on editer.php <?php // check if user edited the page. if(isset($_REQUEST['editfile'])) { $editfile = $_REQUEST['editfile']; $filename = "includes/connDB.php"; $handle = fopen($filename, "w+"); fwrite($handle, $editfile); fclose($handle); echo "Done Editing!"; } // if not show the form else { $self = $_SERVER['PHP_SELF']; echo "<form action='" .$self. "' method='post'/><textarea name='editfile' rows='20' cols='50'>"; $filename = "includes/connDB.php"; $handle = fopen($filename, "r"); $contents = fread($handle, filesize($filename)); echo $contents; fclose($handle); echo "</textarea><input type='submit' value='Submit'/></form>"; } ?> its much simpler Quote Link to comment Share on other sites More sharing options...
doomdude Posted August 14, 2011 Author Share Posted August 14, 2011 Ah that would be the logical way to do it! thank you Quote Link to comment Share on other sites More sharing options...
ZulfadlyAshBurn Posted August 14, 2011 Share Posted August 14, 2011 haha. no prob its my honour to help peeple Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.