Support Forums

Full Version: PHP edit file on server?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello,

I am getting a error when I use a post variable to edit a file on the server.
What I want is to enter the name of the file and then edit it.
Here is my code:

First file "editor.php"
PHP Code:
<form action="editpage-2.php" method="post" />
      <
input type="text" name="page" />  
    <
input type="submit" value="Submit" />  
</
form

Second file "editpage-2.php"
PHP Code:
<?php
$firstfile 
=  $_POST['page'];
echo 
$firstfile;   // the file is shown, and is there
$fn $firstfile;

 
if (isset(
$_POST['content']))
{     
    
$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!");
}
?>
<form action="<?php echo $_SERVER["PHP_SELF"?>" method="post">
    <textarea rows="10" cols="100" name="content"><?php readfile($fn); ?></textarea><br />
    <input type="submit" value="Save" VALUE="Refresh">  
</form> 

The problem is when I hit the save button, it shows this error:


Notice: Undefined index: page in C:\xampp\..\editpage-2.php on line 3

Warning: fopen() [function.fopen]: Filename cannot be empty in C:\xampp...\editpage-2.php on line 11
Error opening file in write mode!

Is there any thing that I missed?

Thank you all,

Inventor
I can seem to find any reference to the "undefined index: page"...
Code:
<form action="<?php echo $_SERVER["PHP_SELF"] ?>" method="post">
    <textarea rows="10" cols="100" name="content"><?php readfile($fn); ?></textarea><br />
    <input type="submit" value="Save" VALUE="Refresh">  
</form>

And yet your script tries to locate it...
PHP Code:
$firstfile =  $_POST['page']; 

Cleared and should work, still not secure though...
Study the script and look at the changes before you just go on copy-pasting...
Code:
<?php

$firstfile =  (isset($_POST['page'])) ? $_POST['page'] : ((isset($_GET['page'])) ? $_GET['page'] : '');
echo $firstfile;   // the file is shown, and is there
$fn = $firstfile; // totally wasting resources and space: lulz


if (isset($_POST['content']))
{    
    $content = stripslashes($_POST['content']);
    $fp = fopen($fn,"w") or die ("Error opening file in write mode!"); // fopen($firstfile) // remove $fn
    fputs($fp,$content);
    
    fclose($fp) or die ("Error closing file!");
}
?>
<form action="<?php echo $_SERVER["PHP_SELF"].'?page={$firstfile}' ?>" method="post">
    <input type="text" name="page" value="{$firstfile}" /><br />
    <textarea rows="10" cols="100" name="content"><?php readfile($fn); ?></textarea><br />
    <input type="submit" value="Save" VALUE="Refresh">  
</form>
It was not a copy paste.
Thank you for helping me!

Edit:
The page is getting the posted variable from another page "editor.php", not from the current page.

$firstfile = $_POST['page']; Is from the post from the editor.php.
You have your editor.php, and then you have the line in code;
PHP Code:
$firstfile =  $_POST['page']; 
This will only work if you press "Submit" on editor.php, however you have another form on editpage-2.php which does submit to it self, $_SERVER['PHP_SELF'], but there is NO $_POST['page'] within this form, therfore you get an error "Undefined index: page"

The code I posted works as intended too, and if it's not what you want then read about using sessions and checking $_POST.
Thank you for helping me! Smile
I didn't know that I needed a second POST in the second form, thanks!
for registration form how to send thanks mail to customer who had enter the form details in our site.

please help me out