Jump to content

If i want to edit a specific file and open it for edit in php


ztealmax

Recommended Posts

hi :)

If i want to edit a specific file and open it for edit in this way:

edit.php?text=news/finall test.txt

how would i implement that in this script?

edit.php
[CODE]
<form action="edit_process.php" method="post">
<textarea rows="1" cols="40" name="title">

</textarea>
<textarea rows="25" cols="40" name="content">
<?
$fn = "news/";
print htmlspecialchars(implode("",file($fn)));
?>
</textarea><br>
<input type="submit" value="Change!">
</form>
[/CODE]

edit_process.php
[code]
<?
$fn = "news/Update" . $_POST['title'];
$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!");
echo "<meta http-equiv=\"refresh\" content=\"0; url=read.php\" />\n";
?>
[/code]
Thanx
//Martin
edit_form.php
[code]<form action="edit_process.php" method="post">
<textarea rows="1" cols="40" name="title">

</textarea>
<textarea rows="25" cols="40" name="content">
<?
$fn = "news/";
print htmlspecialchars(implode("",file($fn)));
?>
</textarea><br>
<input type="submit" value="Change!">
</form>[/code]

edit_process.php
[code]<?
$fn = "news/" . $_POST['title'];
$content = stripslashes($_POST['content']);
$fp = fopen($fn,"a+") or die ("Error opening file in write mode!");
fputs($fp,$content);
fclose($fp) or die ("Error closing file!");
echo "<meta http-equiv=\"refresh\" content=\"0; url=read.php\" />\n";
?>[/code]

no errors att all when i try to open it this way

[b]edit_form.php/?text=news/finall%20test[/b]
just a blank edit form


//Martin
HUUUUUUUUUUUUUUUUUUUUUUUUUUUUGE SECURITY ISSUES HERE! If you're not careful, you could make it so anyone could edit [i]any file[/i] on your server. I would strongly [i]not[/i] recommend ever implementing something like this.

Anyway... Your edit form is probably blank because it looks like you're trying to insert the contents of an invalid file; that is, a directory, just named "news/". Try setting $fn equal to a valid file. Or better yet, never implement something like this in the first place.
[quote] author=Albright link=topic=118593.msg486019#msg486019 date=1166260223]
HUUUUUUUUUUUUUUUUUUUUUUUUUUUUGE SECURITY ISSUES HERE! If you're not careful, you could make it so anyone could edit [i]any file[/i] on your server. I would strongly [i]not[/i] recommend ever implementing something like this.

Anyway... Your edit form is probably blank because it looks like you're trying to insert the contents of an invalid file; that is, a directory, just named "news/". Try setting $fn equal to a valid file. Or better yet, never implement something like this in the first place.
[/quote]

well not quite, im  adding a authenticate thingy later :)

well i was hopping it was possible to open the file like this:

"edit_form.php?text=news/"

Like in my read.php script
[code]<?PHP require_once("theme.php");?>
<?php
// set file to read
$file = $_GET['text'];
// open file
$fh = fopen($file, 'r') /*or die('Could not open file!')*/;
// read file contents
$data = fread($fh, filesize($file)) /*or die('Could not read file!')*/;
// close file
fclose($fh);
// print file contents


?>
<DIV class=spacer>
      <TABLE cellSpacing=0 cellPadding=0>
        <TR>
          <TD class=captiontopleft><IMG style="DISPLAY: block" height=4 alt=""
            src="images/blank.gif" width=21></TD>
          <TD class=captiontopmiddle><IMG style="DISPLAY: block" height=4
            alt="" src="images/blank.gif" width=1></TD>
          <TD class=captiontopright><IMG style="DISPLAY: block" height=4
            alt="" src="images/blank.gif" width=8></TD></TR></TABLE>
      <TABLE cellSpacing=0 cellPadding=0>
       
        <TR>
          <TD class=captionleft><IMG style="DISPLAY: block" height=17 alt=""
            src="images/blank.gif" width=21></TD>
          <TD class=captionbar style="WHITE-SPACE: nowrap"><? echo $file;?></TD>
          <TD class=captionend><IMG style="DISPLAY: block" height=17 alt=""
            src="images/blank.gif" width=21></TD>
          <TD class=captionmain><IMG style="DISPLAY: block" height=17 alt=""
            src="images/blank.gif" width=1></TD>
          <TD class=captionright><IMG style="DISPLAY: block" height=17 alt=""
            src="images/blank.gif" width=9></TD></TR></TABLE>
      <TABLE cellSpacing=0 cellPadding=0>
       
        <TR>
          <TD class=bodyleft><IMG style="DISPLAY: block" height=1 alt=""
            src="images/blank.gif" width=3></TD>
          <TD class=bodymain><DIV class="smallblacktext"><pre>
<? echo $data;?></DIV></pre>
<DIV class=alttd style="TEXT-ALIGN: right"><? echo "Posted: " . date ("F d Y H:i:s.", filemtime($file)); ?>
            </DIV>
</TD>
          <TD class=bodyright><IMG style="DISPLAY: block" height=1 alt=""
            src="images/blank.gif" width=3></TD></TR></TABLE>
      <TABLE cellSpacing=0 cellPadding=0>
       
        <TR>
          <TD class=bottomleft><IMG style="DISPLAY: block" height=8 alt=""
            src="images/blank.gif" width=12></TD>
          <TD class=bottommain><IMG style="DISPLAY: block" height=8 alt=""
            src="images/blank.gif" width=1></TD>
          <TD class=bottomright><IMG style="DISPLAY: block" height=8 alt=""
            src="images/blank.gif" width=12></TD></TR></TABLE></DIV>
[/code]
so what news i select later in a dropdown menu i can link it to that so it opens it for editing, you understand what the hell im talking about ;)

//Thanx
tried this now:

edit.php
[code]<?PHP require_once("theme.php");?>
<form action="edit_process.php" method="post">
<textarea rows="1" cols="40" name="title">

</textarea>
<textarea rows="25" cols="40" name="content">
<?php
// set file to read
$file = $_GET['text'];
// open file
$fn = "news/";
print htmlspecialchars(implode("",file($fn)));
// read file contents
$data = fread($fn, filesize($file)) /*or die('Could not read file!')*/;
// close file
fclose($fn);
// print file contents


?>
</textarea><br>
<input type="submit" value="Change!">
</form>[/code]


edit_process.php
[code]<?
$fn = "news/" . $_POST['title'];
$content = stripslashes($_POST['content']);
$fp = fopen($fn,"a+") or die ("Error opening file in write mode!");
fputs($fp,$content);
fclose($fp) or die ("Error closing file!");
echo "<meta http-equiv=\"refresh\" content=\"0; url=read.php\" />\n";
?>[/code]
well shortly i can get it working..

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.