ZulfadlyAshBurn Posted December 20, 2010 Share Posted December 20, 2010 i am currently creating a school portal. there is this page which enables user to create their own website for Computer Education lesson. Every user are able to upload their files and view but the problem is, i need a function for the user to be able to edit it in on the page itself. Quote Link to comment https://forums.phpfreaks.com/topic/222217-html-source-for-editing/ Share on other sites More sharing options...
Rifts Posted December 20, 2010 Share Posted December 20, 2010 pagelime.com Quote Link to comment https://forums.phpfreaks.com/topic/222217-html-source-for-editing/#findComment-1149554 Share on other sites More sharing options...
Adam Posted December 20, 2010 Share Posted December 20, 2010 "Edit it on the page itself" - what do you mean by this exactly, in-place of each piece of content, or the whole source code is just presented for editing? Â Â pagelime.com Generally I think people ask how to do something on a support forum because they want to implement it themselves. Quote Link to comment https://forums.phpfreaks.com/topic/222217-html-source-for-editing/#findComment-1149556 Share on other sites More sharing options...
ZulfadlyAshBurn Posted December 20, 2010 Author Share Posted December 20, 2010 @Rifts I want to create something like that. I have tried many third party softwares but im still unable to achieve it  @MrAdam What i meant is that they are able to upload their own files and edit them (the files uploaded) on the website itself as a whole source using third party software like tinymce. Quote Link to comment https://forums.phpfreaks.com/topic/222217-html-source-for-editing/#findComment-1149618 Share on other sites More sharing options...
plznty Posted December 21, 2010 Share Posted December 21, 2010 Do you have any PHP knowledge? I don't get what your asking help for? If not then I suggest you learn. Quote Link to comment https://forums.phpfreaks.com/topic/222217-html-source-for-editing/#findComment-1149720 Share on other sites More sharing options...
ZulfadlyAshBurn Posted December 21, 2010 Author Share Posted December 21, 2010 Ys, i have learnt php for quite a long time and have create a website using php, html and mysql. i want users to be able to edit their website source on my page. their code are hosted on the same server. Quote Link to comment https://forums.phpfreaks.com/topic/222217-html-source-for-editing/#findComment-1149801 Share on other sites More sharing options...
Adam Posted December 21, 2010 Share Posted December 21, 2010 You need to read the contents of the file in via a function such as file_get_contents, display it within a textarea, and then overwrite it again using a function such as file_put_contents. You'd need to consider how you'd securely implement it, so they're not able to access each others files. Â You can integrate TinyMCE if you wish, but that's handled/attached client-side (i.e. by JavaScript), the PHP process wouldn't change. Â Your best bet is to approach it one step a time. First get the source in a textarea. Then write it back to the file. Then implement the security. Then look into TinyMCE. Quote Link to comment https://forums.phpfreaks.com/topic/222217-html-source-for-editing/#findComment-1149822 Share on other sites More sharing options...
ZulfadlyAshBurn Posted December 21, 2010 Author Share Posted December 21, 2010 You need to read the contents of the file in via a function such as file_get_contents, display it within a textarea, and then overwrite it again using a function such as file_put_contents. You'd need to consider how you'd securely implement it, so they're not able to access each others files. Â You can integrate TinyMCE if you wish, but that's handled/attached client-side (i.e. by JavaScript), the PHP process wouldn't change. Â Your best bet is to approach it one step a time. First get the source in a textarea. Then write it back to the file. Then implement the security. Then look into TinyMCE. Â how to i echo the source in to textarea? this is what i amble to docan you help me with the teaxtarea? <?php $file = file_get_contents ('index.php'); Echo $file; ?> Quote Link to comment https://forums.phpfreaks.com/topic/222217-html-source-for-editing/#findComment-1149825 Share on other sites More sharing options...
ZulfadlyAshBurn Posted December 21, 2010 Author Share Posted December 21, 2010 ive got it. was quite a simple code. why didnt i think of tht. omg! <?php $file = file_get_contents ('index2.php', true); ?> <textarea name="content" cols="50" rows="15"><?php echo $file; ?></textarea> Â Quote Link to comment https://forums.phpfreaks.com/topic/222217-html-source-for-editing/#findComment-1149829 Share on other sites More sharing options...
Adam Posted December 21, 2010 Share Posted December 21, 2010 You just quite literally echo it between the tags. In-case there's a closing </textarea> within the source however, you need to escape the data to entities with htmlentities: Â <textarea><?php echo htmlentities($str); ?></textarea> Â The mark-up will still appear the same within the textarea, however if you look at the source you'll notice - for example - a <p> tag looks like <p>. This means when the user saves, you need to decode the data before you write it back to the file with html_entity_decode. Quote Link to comment https://forums.phpfreaks.com/topic/222217-html-source-for-editing/#findComment-1149831 Share on other sites More sharing options...
ZulfadlyAshBurn Posted December 21, 2010 Author Share Posted December 21, 2010 when the user save, it saves the file but when it is viewed, it looks different o.0 Â edit.php <?php $file = file_get_contents ('index2.php', true); ?> <html> <head> <title>Editor</title> <script type="text/javascript" src="ckeditor/ckeditor.js"></script> </head> <body> <form method="post" action="save.php"> <p> My Editor:<br /> <textarea name="editor1"><?php echo htmlentities($file); ?></textarea> <script type="text/javascript"> window.onload = function() { CKEDITOR.replace( 'editor1' ); }; </script> </p> <p> <input type="submit" /> </p> </form> </body> </html> Â save.php <?php $editor_data = $_POST[ 'editor1' ]; $a = htmlentities($editor_data); $b = html_entity_decode($a); $fileurl = "index2.php"; $fp = fopen($fileurl, 'w'); fwrite($fp, $b); fclose($fp); ?> Â Quote Link to comment https://forums.phpfreaks.com/topic/222217-html-source-for-editing/#findComment-1149835 Share on other sites More sharing options...
Adam Posted December 21, 2010 Share Posted December 21, 2010 $editor_data = $_POST[ 'editor1' ]; $a = htmlentities($editor_data); $b = html_entity_decode($a); Â The problem is that you're encoding the encoded data (so it's encoded twice), then decoding it back to the once encoded data. You have to remember that $_POST['editor1'] has already been encoded, you just need to decode it: Â <?php // get the encoded data $editor_data = $_POST[ 'editor1' ]; // decode it $editor_data = html_entity_decode($editor_data); // set filename $fileurl = "index2.php"; // write to file file_put_contents($fileurl, $editor_data); ?> Quote Link to comment https://forums.phpfreaks.com/topic/222217-html-source-for-editing/#findComment-1149837 Share on other sites More sharing options...
ZulfadlyAshBurn Posted December 21, 2010 Author Share Posted December 21, 2010 the page still fails. Quote Link to comment https://forums.phpfreaks.com/topic/222217-html-source-for-editing/#findComment-1149839 Share on other sites More sharing options...
Adam Posted December 21, 2010 Share Posted December 21, 2010 Can you elaborate on "fails"? Quote Link to comment https://forums.phpfreaks.com/topic/222217-html-source-for-editing/#findComment-1149840 Share on other sites More sharing options...
ZulfadlyAshBurn Posted December 21, 2010 Author Share Posted December 21, 2010 actual file http://zulfadlyashburn.com/ons/index.php  after edited http://zulfadlyashburn.com/ons/index2.php Quote Link to comment https://forums.phpfreaks.com/topic/222217-html-source-for-editing/#findComment-1149842 Share on other sites More sharing options...
Adam Posted December 21, 2010 Share Posted December 21, 2010 You have magic quotes enabled, a deprecated feature of PHP. The manual explains how to disable it. Quote Link to comment https://forums.phpfreaks.com/topic/222217-html-source-for-editing/#findComment-1149844 Share on other sites More sharing options...
ZulfadlyAshBurn Posted December 21, 2010 Author Share Posted December 21, 2010 thanks alot. i works Quote Link to comment https://forums.phpfreaks.com/topic/222217-html-source-for-editing/#findComment-1149847 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.