ngreenwood6 Posted August 4, 2008 Share Posted August 4, 2008 I am trying to create a backend for my website. I would like to be able to edit certain pages from my backend. I would like to know how to show the code on a page for a certain file so that I can edit the page from my site. If anyone can help it would be appreciated. If you need more info please let me know. Quote Link to comment Share on other sites More sharing options...
trq Posted August 4, 2008 Share Posted August 4, 2008 Read the file into a variable, display that variable within a html textarea. When the form (textarea) is submitted write the contents back to the file. Quote Link to comment Share on other sites More sharing options...
ngreenwood6 Posted August 4, 2008 Author Share Posted August 4, 2008 Sorry I am noob as you can tell. How can I read a file into a variable? maybe an example. Quote Link to comment Share on other sites More sharing options...
trq Posted August 4, 2008 Share Posted August 4, 2008 Take a look at file_get_contents() and file_put_contents(). Quote Link to comment Share on other sites More sharing options...
ngreenwood6 Posted August 4, 2008 Author Share Posted August 4, 2008 I have come up with this code: <?php $fopen = fopen("index.php", "rb"); $default = fread($fopen); ?> <html> <title>Read Data</title> <form name="change" method="post" action="<?php echo $PHP_SELF; ?>"> <input type="textarea" value="$default"> </form> </html> However, It is giving me an error "Warning: Wrong parameter count for fread() in C:\wamp\www\login\edit.php on line 4". At this point I was just trying to open the file into a text area. Also it is just displaying default in the textarea. Anyone help? Quote Link to comment Share on other sites More sharing options...
ngreenwood6 Posted August 4, 2008 Author Share Posted August 4, 2008 Actually I have just come up with this: <?php $index = file_get_contents("index.php"); ?> <html> <title>Read Data</title> <form name="change" method="post" action="<?php echo $PHP_SELF; ?>"> <textarea name="textarea" cols="100" rows="100" value="<? echo $index; ?>"> </form> </html> It displays my index page in the textarea. However, I do not know how I would code it to save the changes. If anyone can help it is appreciated. Quote Link to comment Share on other sites More sharing options...
dezkit Posted August 4, 2008 Share Posted August 4, 2008 <?php $file_name = "index.php"; $index = file_get_contents("$file_name"); if(isset($_POST['submit'])){ file_put_contents($textarea, $file_name); echo "page has been updated."; } else { ?> <html> <title>Read Data</title> <form name="change" method="post" action="<?php echo $PHP_SELF; ?>"> <textarea name="textarea" cols="100" rows="100" value="<?php echo $index; ?>"></textarea><br> <input type="submit" value="submit"><input type="reset" value="reset"> </form> </html> <?php } ?> i think. Quote Link to comment Share on other sites More sharing options...
trq Posted August 4, 2008 Share Posted August 4, 2008 Firstly, $PHP_SELF has long been depricated, don't use it. <?php if (isset($_POST['submit'])) { file_put_contents('index.php', $_POST['textarea']); } else { $index = file_get_contents("index.php"); ?> <html> <title>Read Data</title> <form name="change" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>"> <textarea name="textarea" cols="100" rows="100" value="<? echo $index; ?>"> <input type="submit" name="submit"> </form> </html> <?php } ?> Be aware that this is just an example. Allowing users to write to your index.php file like that is very insecure. You really want to have all this locked behind a login system of some sort at the very least. Quote Link to comment Share on other sites More sharing options...
ngreenwood6 Posted August 4, 2008 Author Share Posted August 4, 2008 Thanks for the replys. I tried both of your codes, but I am getting this in my text area: <input type="submit" name="submit"> </form> </html> And it is not showing the submit button. Quote Link to comment Share on other sites More sharing options...
dezkit Posted August 4, 2008 Share Posted August 4, 2008 you missed a </textarea> but this still doesn't work for me though. any ideas? maybe its my server? @ngreen <?php if (isset($_POST['submit'])) { file_put_contents('index.php', $_POST['textarea']); } else { $index = file_get_contents("index.php"); ?> <html> <title>Read Data</title> <form name="change" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>"> <textarea name="textarea" cols="100" rows="100" value="<? echo $index; ?>"></textarea> <input type="submit" name="submit"> </form> </html> <?php } ?> is the correct code Quote Link to comment Share on other sites More sharing options...
ngreenwood6 Posted August 4, 2008 Author Share Posted August 4, 2008 I test it on just a text file and it posted it but it did not show it in the textarea. Quote Link to comment Share on other sites More sharing options...
trq Posted August 4, 2008 Share Posted August 4, 2008 <textarea name="textarea" cols="100" rows="100" value="<?php echo $index; ?>"></textarea> Quote Link to comment Share on other sites More sharing options...
dezkit Posted August 4, 2008 Share Posted August 4, 2008 i dont know then, you gotta use a mysql database then bro Quote Link to comment Share on other sites More sharing options...
ngreenwood6 Posted August 4, 2008 Author Share Posted August 4, 2008 I have that code in there but it is still not showing the file in the text area, but if I input data to the field it puts it into the text file. Any suggestions? Quote Link to comment Share on other sites More sharing options...
ngreenwood6 Posted August 4, 2008 Author Share Posted August 4, 2008 actually it works with a file ending in .php but with the file ending in .txt it isn't working. Anyone have any idea why not? Quote Link to comment Share on other sites More sharing options...
trq Posted August 4, 2008 Share Posted August 4, 2008 Sorry, should have picked this up. There is no such thing as a value attribute for a textarea. You'll also want to use htmlentities(). <?php if (isset($_POST['submit'])) { file_put_contents('index.php', $_POST['textarea']); } else { $index = file_get_contents("index.php"); ?> <html> <title>Read Data</title> <form name="change" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>"> <textarea name="textarea" cols="100" rows="100"><?php echo htmlentities($index); ?></textarea> <input type="submit" name="submit"> </form> </html> <?php } ?> Quote Link to comment Share on other sites More sharing options...
dezkit Posted August 4, 2008 Share Posted August 4, 2008 @thorpe it doesnt save :/ Quote Link to comment Share on other sites More sharing options...
trq Posted August 4, 2008 Share Posted August 4, 2008 Put this at the top. <?php error_reporting(E_ALL); ini_set('display_errors','1'); ?> You likely have permissions issues. Quote Link to comment Share on other sites More sharing options...
dezkit Posted August 4, 2008 Share Posted August 4, 2008 it says Fatal error: Call to undefined function: file_put_contents() in /update.php on line 6 Quote Link to comment Share on other sites More sharing options...
trq Posted August 4, 2008 Share Posted August 4, 2008 What version of php are you using? Quote Link to comment Share on other sites More sharing options...
dezkit Posted August 4, 2008 Share Posted August 4, 2008 PHP Version 4.3.11 Quote Link to comment Share on other sites More sharing options...
DarkWater Posted August 4, 2008 Share Posted August 4, 2008 I'd seriously suggest upgrading to PHP 5. Quote Link to comment Share on other sites More sharing options...
trq Posted August 4, 2008 Share Posted August 4, 2008 Theres your problem, your using an out of date version. You'll need to use fopen() and fwrite(0 though Id also upgrade to the current version of php. (5.2.6 I believe). Quote Link to comment Share on other sites More sharing options...
dezkit Posted August 4, 2008 Share Posted August 4, 2008 darn, are there any really cheap and very good hosting companies that support the latest php versions? at the moment i am using yahoo smallbusiness (bad) Quote Link to comment Share on other sites More sharing options...
ngreenwood6 Posted August 4, 2008 Author Share Posted August 4, 2008 Thanks this works for me. Now I have another quick question. After it is submitted I would like for it to go back to the same page but show it with the updated information. Is this the correct code? <?php if (isset($_POST['submit'])) { file_put_contents('test.php', $_POST['textarea']); echo "<a href='edit.php'>Go Back</a>"; } else { $index = file_get_contents("test.php"); ?> <html> <title>Read Data</title> <form name="change" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>"> <textarea name="textarea" cols="80" rows="20"><?php echo htmlentities($index); ?></textarea> <input type="submit" name="submit"> </form> </html> <?php } ?> 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.