skimppimppin along! NEWS   FORUM   DOWNLOAD   LINKS    
HOME ACCOUNT PRIVATE MESSAGE  
Login
Username:

Password:


Lost Password?

Register now!

Main Menu

Search

skimpydog.com Forum Index
   Scripting
  php - editing text files on a server

Browsing this Thread:   1 Anonymous Users

 

 Bottom   Previous Topic   Next Topic
  •  Rate Thread
      Rate this Thread
      Excellent
      Good
      Average
      Bad
      Terrible
Poster Thread
php - editing text files on a server
#1
Webmaster
Joined: 2007/6/10
From Dallas, TX
Posts: 79
Level : 7
HP : 0 / 170
MP : 26 / 8767
EXP : 83
Group:
Webmasters
Registered Users
Offline
I cannot stress enough that if you use this script to make sure the script in in a secure area, with preferably limited access rights from the outside world! - This should be encrypted traffic, and secured with user authentication, and session handling. I take no responsibility if your server get's hacked because you do not know what you are doing.

This is a very basic script that should be inserted with a header, and footer to handle cookie sessions and user authentication at a minimum! Preferably even with SSL so if your traffic is sniffed, still only you know of the file.

The scope of this script is to be able to view, and edit a flat text file, while keeping the structure of the file intact by means of <textarea>.


The file you want to edit needs to be editable by the webuser - This is the user which is running the web server daemon, normally on apache, this is the user "nobody".
-- This is considered insecure!

Normally the file(s) you want to edit must contain the permissions of "666", such as:
chmod 666 /path/to/somefile.txt

This does however depend on the server configuration.
This script works with "regester_globals = off" in your php.ini.

Then create the php page with the following:

<align=left>Edit that file!!</align><br><br>
<?php
$loadcontent = "/path/to/somefile.txt";
if(@$_POST['save_file']) {
$savecontent = stripslashes($_POST['savecontent']);
$fp = @fopen($loadcontent, "w") or die ("Cannot open file for reading!");
if ($fp) {
fwrite($fp, $savecontent);
fclose($fp) or die ("cannot Close File because it was never opened!");
print '<a href='.$_SERVER[PHP_SELF].'>Refresh</a>';
print "<html><head><META http-equiv=\"refresh\" content=\"0;URL=$_SERVER[PHP_SELF]\"></head><body>";
}
}
$fp = @fopen($loadcontent, "r");
 $loadcontent = @fread($fp, filesize($loadcontent)) or die ("Cannot calculate file size for reading because I cannot read the file!!");
$lines = explode("\n", $loadcontent);
$count = count($lines);
$loadcontent = htmlspecialchars($loadcontent);
fclose($fp);
for ($a = 1; $a < $count+1; $a++) {
$lines .= "$a\n";
}
?>
<form method=post action="<?php $_SERVER['PHP_SELF']?>">
<input type="submit" name="save_file" value="Save">
<table width="100%" valign="top" border="0" cellspacing="1" cellpadding="1">
<tr>
<td width="3%" align="right" valign="top"><pre style="text-align: right; padding: 4px; overflow: auto; border: 0px groove; font-size: 12px" name="lines" cols="4" rows="<?=$count
+3;?>"><?php $line;?></pre></td>
<td width="97%" align="left" valign="top"><textarea style="text-align: left; padding: 0px; overflow: auto; border: 3px groove; font-size: 12px" name="savecontent" cols="150" row
s="<?php $count;?>" wrap="OFF"><?php echo $loadcontent; ?></textarea></td>
</tr>
</table>
<br>
<input type="submit" name="save_file" value="Save">
</form>
Posted on: 2008/12/20 19:36
Create PDF from Post Print
Top
 Top   Previous Topic   Next Topic

 


 You cannot start a new topic.
 You can view topic.
 You cannot reply to posts.
 You cannot edit your posts.
 You cannot delete your posts.
 You cannot add new polls.
 You cannot vote in polls.
 You cannot attach files to posts.
 You cannot post without approval.
Links