indiWiz.com
SiteSearch:
Jump:

Site Map | WizTools.org | jCraze Blog
Web Developer's Den!
Home : WDD : ServerSide
This Article...
Print Version
Add Comment
View Comments
ServerSide
Apache on Linux
Apache User Directory Configuration
PostgreSQL Configuration
CGI: What and How?
PHP Jump Start Guide
Arrays in PHP
PHP and HTML Forms
Variable Passing in PHP
PHP 'include'
Drop-down Menu Navigation in PHP
Edit Your Site Online Using PHP
PHP MySQL Polling System
Dynamic, Search Engine Friendly Pages
Quot Ticker Using SSI
Regular Expressions Reference
PHP File Browser
PHP DB Deletion/Approval Script
Sections

Commniquè
Sign Guestbook!
Read Guestbook!
MailMe!

Edit Your Site Online (using PHP)

During the infancy of indiWiz.com, the first page of the site 'index.php' was almost a static one. The only PHP thing in it was a basic counter. There was, and is, the editorial 'From Subhash...' which was frequently updated. So what I did was upload (through FTP) this whole file 'index.php' to change the editorial content. Later I shifted the editorial content to a separate file 'temp.html' and included it dynamically in 'index.php' using the 'include' statement. But even this procedure required me to upload the file 'temp.html' using FTP. To overcome this ineffeciency, I wrote a program to edit the file 'temp.html' online. The working of this program is explained below.

This program at first fetches the page to be edited and prints the content of this page on a text field. You have to type the new text or edit the original one in this text field. Whenever you press the 'submit' button of the form, the changed text is inserted to the file.

Code I: To Print The Content in a Text Field

<form action="processscript.php" method="post">
<textarea rows="25" cols="80" name="content">
<?
$fn = "relative/or/abs/path/to/file.ext";
print htmlspecialchars(implode("",file($fn)));
?>
</textarea><br>
<input type="submit" value="Change!">
</form>

This PHP code uses three functions:

  • file(): This function returns the contents of the file (file name is passed as parameter) as an array.

  • implode(): This function combines the array elements into one huge variable and returns it. The first parameter specifies the divide character which should be used to separate the array elements. In our example we have given the string null character. The second parameter takes the array name.

  • htmlspecialchars(): This function converts all HTML special characters like & to &amp; etc. This is necessary for displaying the file contents in the <textarea>.

Note: 'file.ext' should be writable. Give 'chmod 666' command to this file if you are using a UNIX/Linux server.

Code II: To Insert the Changed Text

<?
$fn = "relative/or/abs/path/to/file.ext";
$content = stripslashes($_POST['content']);
$fp = fopen($fn,"w") or die ("Error opening file in write mode!");
fputs($fp,$content);
fclose($fp) or die ("Error closing file!");
?>

I pass the $_POST['content'] variable through the stripslashes() because I noticed that slashes are automatically added to some special characters (like: ' ;) in the content passed through GET/POST in certain configuration. The other statements must be familiar to C programmers: we open the file in write mode (the 'w' parameter in the fopen() function). Then we write the new content $content to the file using fputs(). At last we close the file. As simple as that!


User Comments

Add Comment

[Quick Stats: Number of main threads: 2, Number of sub-threads: 1]


Sign Guestbook | Who is Subhash?
The contents of this site are copyright© 2000-2008, indiWiz.com. All Rights Reserved.