hi, im trying to learn php i want to do this...
say i got 5 .txt files on my site, and on the page w/ html i have include cmds to include the .txt to that page so all i have to do is edit the text to change the site... now, how can i make like a little script so i can edit them thru web browser rather then uploading thru ftp.. all i need is a save function nothing fancy (that's why i dont want to us a file manager)
i have 10 tex files i'd like to be able to edit,
any help would be greatly appreciated.
how to edit a text file thru php/broswer
Moderator: General Moderators
I've done this before and it is quite easy to do. I'll paste it below.
The one below uses a mysql datase to store the same data that u have in ur text pages, but u can easily modify this with fopen to work for your text files.
I'm too lazy to change it for you.
Hope it helps, though
The database schema is :
CREATE TABLE html (
type varchar(30) unique NOT NULL,
data text,
);
'type' is like the filename eg. 'home' or 'links', etc..... and has to be unique
'data' is the data you normally hold in the page
the php file starts here :
<html><BODY BGCOLOR="#ddddff"><center><BR>
<?
// ------------------------------ SECTION 1 : login to db ------------------------------------
$dbname = "somedbname"; // edit this urself
mysql_connect("localhost") or die("Could not connect: " . mysql_error());
mysql_select_db( $dbname ) or die( "database not selected");
// ------ make any updates if this file was called with another form from this file ----- //
if ($delete && $selection != "") {
$result = mysql_query("delete from html where type = \"$selection\"");
}elseif ($insert && $insert != ""){
$result = mysql_query("insert into html (type,data) values (\"$value\",'')");
}elseif ($update || $updateAndEdit) {
$result = mysql_query("update html set data = \"$updateddata\" where type = \"$updatetype\"");
}
?>
<?
// ------------------------------ SECTION 2 : create form ------------------------------------
// ------FORMS for editing/deleting/inserting ----- // ?>
<TABLE borderColor=gray cellPadding=3 border=2><TR><TD >
<table border = 0><tr><td>
<? // ------ FORM1: create delete & edit form ------ // ?>
<form action = "" method = "post">
<select name="selection" width = 50>
<option value=""></option>
<?
$result = mysql_query("SELECT type from html order by type");
while ($row = mysql_fetch_array($result))
print "<option value=\"$row[type]\">$row[type]</option>\n";
?>
</select>
<input type = "submit" name = "delete" value = "Delete ">
<input type = "submit" name = "edit" value = " Edit ">
</form>
<? // ------ FORM 2: create insert form ------ // ?>
<td width = 16><td>
<form action = "" method = "post">
<input type = "text" size = 12 name = "value" value = "" >
<input type = "submit" name = "insert" value = "Insert" >
</form>
</table></table>
<?
// ------------------------------ SECTION 2 : create form type selected ------------------------------------
// ------ forms for updating and editing ----- //
if ($delete && $selection != ""){
print "<BR><BR><b>deleted $selection</b>";
}elseif ($insert && $insert != ""){
print "<BR><BR><b>inserted $value</b>";
}elseif ($update) {
print "<BR><BR><b>updated $updatetype</b>";
}elseif($edit && $selection != ""){
$result = mysql_query("select * from html where type = \"$selection\"");
$row = mysql_fetch_array($result);
?>
<form action = "" method = "post">
<TEXTAREA name="updateddata" rows="18" cols="82"><? print $row[data]?></TEXTAREA>
<input type = "hidden" name = "updatetype" value = "<? print $selection ?>">
<BR><BR>
<input type = "submit" name = "update" value = "update & finish" >
<input type = "submit" name = "updateAndEdit" value = "update & edit more" >
<input type = "submit" name = "" value = "cancel update" >
</form>
<BR><BR>
<?
}elseif($updateAndEdit && $updatetype != ""){
$result = mysql_query("select * from html where type = \"$updatetype\"");
$row = mysql_fetch_array($result);
?>
<form action = "" method = "post">
<TEXTAREA name="updateddata" rows="18" cols="82"><? print $row[data]?></TEXTAREA>
<input type = "hidden" name = "updatetype" value = "<? print $updatetype ?>">
<BR><BR>
<input type = "submit" name = "update" value = "update & finish" >
<input type = "submit" name = "updateAndEdit" value = "update & edit more" >
<input type = "submit" name = "" value = "cancel update" >
</form>
<BR><BR>
<?
}
?>
</body></html>
The one below uses a mysql datase to store the same data that u have in ur text pages, but u can easily modify this with fopen to work for your text files.
I'm too lazy to change it for you.
Hope it helps, though
The database schema is :
CREATE TABLE html (
type varchar(30) unique NOT NULL,
data text,
);
'type' is like the filename eg. 'home' or 'links', etc..... and has to be unique
'data' is the data you normally hold in the page
the php file starts here :
<html><BODY BGCOLOR="#ddddff"><center><BR>
<?
// ------------------------------ SECTION 1 : login to db ------------------------------------
$dbname = "somedbname"; // edit this urself
mysql_connect("localhost") or die("Could not connect: " . mysql_error());
mysql_select_db( $dbname ) or die( "database not selected");
// ------ make any updates if this file was called with another form from this file ----- //
if ($delete && $selection != "") {
$result = mysql_query("delete from html where type = \"$selection\"");
}elseif ($insert && $insert != ""){
$result = mysql_query("insert into html (type,data) values (\"$value\",'')");
}elseif ($update || $updateAndEdit) {
$result = mysql_query("update html set data = \"$updateddata\" where type = \"$updatetype\"");
}
?>
<?
// ------------------------------ SECTION 2 : create form ------------------------------------
// ------FORMS for editing/deleting/inserting ----- // ?>
<TABLE borderColor=gray cellPadding=3 border=2><TR><TD >
<table border = 0><tr><td>
<? // ------ FORM1: create delete & edit form ------ // ?>
<form action = "" method = "post">
<select name="selection" width = 50>
<option value=""></option>
<?
$result = mysql_query("SELECT type from html order by type");
while ($row = mysql_fetch_array($result))
print "<option value=\"$row[type]\">$row[type]</option>\n";
?>
</select>
<input type = "submit" name = "delete" value = "Delete ">
<input type = "submit" name = "edit" value = " Edit ">
</form>
<? // ------ FORM 2: create insert form ------ // ?>
<td width = 16><td>
<form action = "" method = "post">
<input type = "text" size = 12 name = "value" value = "" >
<input type = "submit" name = "insert" value = "Insert" >
</form>
</table></table>
<?
// ------------------------------ SECTION 2 : create form type selected ------------------------------------
// ------ forms for updating and editing ----- //
if ($delete && $selection != ""){
print "<BR><BR><b>deleted $selection</b>";
}elseif ($insert && $insert != ""){
print "<BR><BR><b>inserted $value</b>";
}elseif ($update) {
print "<BR><BR><b>updated $updatetype</b>";
}elseif($edit && $selection != ""){
$result = mysql_query("select * from html where type = \"$selection\"");
$row = mysql_fetch_array($result);
?>
<form action = "" method = "post">
<TEXTAREA name="updateddata" rows="18" cols="82"><? print $row[data]?></TEXTAREA>
<input type = "hidden" name = "updatetype" value = "<? print $selection ?>">
<BR><BR>
<input type = "submit" name = "update" value = "update & finish" >
<input type = "submit" name = "updateAndEdit" value = "update & edit more" >
<input type = "submit" name = "" value = "cancel update" >
</form>
<BR><BR>
<?
}elseif($updateAndEdit && $updatetype != ""){
$result = mysql_query("select * from html where type = \"$updatetype\"");
$row = mysql_fetch_array($result);
?>
<form action = "" method = "post">
<TEXTAREA name="updateddata" rows="18" cols="82"><? print $row[data]?></TEXTAREA>
<input type = "hidden" name = "updatetype" value = "<? print $updatetype ?>">
<BR><BR>
<input type = "submit" name = "update" value = "update & finish" >
<input type = "submit" name = "updateAndEdit" value = "update & edit more" >
<input type = "submit" name = "" value = "cancel update" >
</form>
<BR><BR>
<?
}
?>
</body></html>
nah .. just the one table created once, but a different entry/insert for each page/file that you'd have included from each include file u had.
CREATE TABLE html (
type varchar(30) unique NOT NULL,
data text,
);
So, lets say you currently include from links.txt
then you'd add to the "html" table like so :
$type = 'links';
$data = "ur data here .. can be <br>over many lines<BR> .... etc";
$result = mysql_query("INSERT INTO html VALUES ('$type', '$data')") or die("DB INSERT Error");
and do the same for other included files different to 'links'
And in the place where u had
include(links.txt);
u need :
$result = mysql_query("SELECT data from html where type = \"links\"");
$row = mysql_fetch_array($result);
$links = $row[data];
print $links;
or u can make a function that takes in "links" and outputs "print $links" which I just realised would make my code much shorter and simpler =)
when u have multiple pages included
CREATE TABLE html (
type varchar(30) unique NOT NULL,
data text,
);
So, lets say you currently include from links.txt
then you'd add to the "html" table like so :
$type = 'links';
$data = "ur data here .. can be <br>over many lines<BR> .... etc";
$result = mysql_query("INSERT INTO html VALUES ('$type', '$data')") or die("DB INSERT Error");
and do the same for other included files different to 'links'
And in the place where u had
include(links.txt);
u need :
$result = mysql_query("SELECT data from html where type = \"links\"");
$row = mysql_fetch_array($result);
$links = $row[data];
print $links;
or u can make a function that takes in "links" and outputs "print $links" which I just realised would make my code much shorter and simpler =)
when u have multiple pages included
- Sevengraff
- Forum Contributor
- Posts: 232
- Joined: Thu Apr 25, 2002 9:34 pm
- Location: California USA
- Contact:
why does the script connect to MySQL when your just editing .txt files? I think i have a script that will do what you want wickedest. try this:
editor.php:
its not the best one, but it works when i use it. to edit a file, just put ?file=name.txt in the url. like http://www.domain.com/you/editor.php?file=config.txt
editor.php:
Code: Select all
<?php
// file editor by sevengraff
// sevengraff@yahoo.com
if(isset($_REQUESTї'file'])) { $file = $_REQUESTї'file']; $a = "open"; }
if(isset($_REQUESTї'save'])) { $a = "write"; }
if(!isset($a)) { $a = "msg";}
switch($a) {
case "open":
?>
<iframe src="<?=$file; ?>" width="100%" heigth="300"></iframe>
<?
$fp = fopen($file , 'r');
$content = fread($fp, filesize($file));
$content = ereg_replace("<", "<", $content);
$content = ereg_replace(">", ">", $content);
fclose($fp);
?>
<form method="post" action="editor.php">
File: <input type="text" name="file" value="<?php echo $file?>" class="border" /><br />
Content:<br />
<textarea NOWRAP rows="15" cols="90" name="content" class="border"><?php echo $content?></textarea><br>
<input name="save" type="submit" value="Save" class="button"><input type="reset" value="Clear" class="button">
</form>
<?
break;
case "msg":
echo("you did not specify a file.");
break;
case "write":
$content = stripslashes($_POSTї'content']);
$content = ereg_replace("<", "<", $content);
$content = ereg_replace(">", ">;", $content);
$fp = fopen($file , 'w');
fwrite($fp, $content);
fclose($fp);
echo("File Updated <br> <a href="$file">Goto File</a><br>
<a href="editor.php?file=$file">Edit file again</a><br>
<b>The File:</b><hr>");
include("$file");
break;
}
?>- Sevengraff
- Forum Contributor
- Posts: 232
- Joined: Thu Apr 25, 2002 9:34 pm
- Location: California USA
- Contact: