Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
PHP edit file on server?
#1
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
Reply
#2
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>
Reply
#3
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.
Reply
#4
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.
Reply
#5
Thank you for helping me! Smile
I didn't know that I needed a second POST in the second form, thanks!
Reply
#6
for registration form how to send thanks mail to customer who had enter the form details in our site.

please help me out
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  PHP Framework List: An Ultimate Guide to 102 PHP Frameworks for Web Developers tk-hassan 0 758 07-27-2020, 11:26 PM
Last Post: tk-hassan
  Use PHP in a text file? Blic 4 1,219 10-25-2011, 08:12 PM
Last Post: Project Evolution
  PHP Video Tutorials (PHP For Beginners) Eleqtriq 4 3,233 10-10-2011, 01:00 PM
Last Post: Greyersting
  help : PHP : Executing .jar file killer_03 0 1,024 03-12-2011, 11:09 PM
Last Post: killer_03
  Common PHP and SQL [File-Sharing Site] Project Evolution 3 1,664 12-12-2010, 02:47 AM
Last Post: Bursihido

Forum Jump:


Users browsing this thread: 1 Guest(s)