By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
429,045 Members | 1,305 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 429,045 IT Pros & Developers. It's quick & easy.

How to open and edit a file on the server

P: 23
Hello all

I would love to know if any body has a knowledge of opening a file like a html or a css file on the server and edit the file, after editing, perform a save on the file.

Example like in joomla where one can open the html pages in a textbox and edit the file and save the file again.

I would love to know how this could be done in php

Any help will realy be appreciated.
Jan 23 '12 #1

✓ answered by Dormilich

PHP can open and save a file (file_get_contents(), file_put_contents()) but editing you have to do yourself. and since PHP is not an editor, you have to pass the data to an application that can handle the editing.

Share this Question
Share on Google+
6 Replies


Dormilich
Expert Mod 5K+
P: 8,639
PHP can open and save a file (file_get_contents(), file_put_contents()) but editing you have to do yourself. and since PHP is not an editor, you have to pass the data to an application that can handle the editing.
Jan 23 '12 #2

P: 23
@Dormilich
Thank you very much for the snippet but how ever i am a little confused on how to go about this.

Example i have a page called test.php on the same diretory as my current php page, i would like to output it in a texteditor and when i edit the outputed content and submit the form how do i write my php code to save the new file using the same mane as test.php in the same directory. thank you very much.
Jan 23 '12 #3

Dormilich
Expert Mod 5K+
P: 8,639
that#s quite simple. when you send the edited content back to PHP, you get the sent string via GET/POST and (after verifying that the content is trustworthy) you can save it to disk. you can send the filename with this form as well so you don't have to lock your application.

personally, I recommend to allow this only for logged in users, otherwise everyone could edit files on your server.
Jan 23 '12 #4

P: 23
okay i tried file_get_content its only displaying the body part of the html code. I would like it to show the source code too including the scripts and head tags. heres my sample php code

Expand|Select|Wrap|Line Numbers
  1. <?php
  2. $t = file_get_contents('mypage.html');
  3. ?>
  4.  
  5. <form>
  6. <textarea><?php echo html_entity_decode($t); ?></textarea>
  7. <input type="submit" Value="submit" />
I would like the whole page content including the doctype,html,head,title and scripts tags to display in the textarea.

Thank you.
I appreciate it
Jan 23 '12 #5

Dormilich
Expert Mod 5K+
P: 8,639
what does var_dump($t) give you?
Jan 23 '12 #6

P: 23
@Dormilich
Hey thank you soooooooooooooooooooooooo much i found that very helpful

I appreciate it.
Jan 23 '12 #7

Post your reply

Sign in to post your reply or Sign up for a free account.