Editing Files Using POST Forms

From: Date: Wed, 18 Jul 2001 17:16:22 +0000
Subject: Editing Files Using POST Forms
Groups: php.general 
Request: Send a blank email to php-general+get-58480@lists.php.net to get a copy of this message
I have created a php page that accepts a variable called $file using GET. It then displays this in a textarea box. You can then edit the file and click a button to save it and it POSTS it to the same page (edit.php) on the server. This works but it adds slashes before any speech marks (""). Can somebody help me with this. Below is an example of how it changes the code and the code for the page edit.php Regards, Corin Rathbone www.corin.org.uk --------------------------------- A page before editing: <html> <head> <title></title> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> <meta name="author" content="Corin Rathbone 2001"> </head> <body> <div class="heading">Delete A File</div> </body> </html> --------------------------------- The same page after editing: <!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\"> <html> <head> <title></title> <meta http-equiv=\"Content-Type\" content=\"text/html; charset=iso-8859-1\"> <meta name=\"author\" content=\"Corin Rathbone 2001\"> </head> <body> <div class=\"heading\">Delete A File</div> </body> </html> --------------------------------- The edit.php page source: <?php include "config.php"; //Start Pages PHP here if(isset($save)){ $fp = fopen("$file", "wb") or die("Could not open file!"); $worked = fwrite($fp, $contents) or die("Could not write file!"); fclose($fp) or die("Could not close file!"); $edit=false; $save=true; } elseif(isset($file)){ $fp = fopen("$file", "rb") or die("Could not open file!"); $contents = fread($fp, filesize($file)) or die("Could not read file!"); fclose($fp) or die("Could not close file!"); $edit=true; $save=false; } else{ $edit=false; $save=false; } //End Pages PHP Here ?> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <title>Edit A File - File Explorer For <?php print($root_url);?></title> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> <meta name="author" content="Corin Rathbone webmaster@corirn.org.uk"> <meta name="copyright" content="Corin Rathbone 2001"> <link rel="stylesheet" type="text/css" href="main.css"> </head> <body> <!-- Start Of Page Content --> <div class="heading">Edit A File</div> <?php if($edit){ print( "Contents Of file: <b>" ); print( "$file" ); print( "</b>\n" ); print( "<p align=\"center\">\n" ); print( " <form action=\"edit.php\" method=\"post\" >\n" ); print( "<input type=\"hidden\" name=\"file\" value=\"" ); print( "$file" ); print( "\">\n" ); print( "<input type=\"hidden\" name=\"save\" value=\"1\">" ); print( " <textarea name=\"contents\" rows=\"25\" cols=\"90\">\n" ); print( "$contents" ); print( " </textarea><br>\n" ); print( " <input type=\"submit\" value=\"Save\">\n" ); print( " </form>\n" ); print( "</p>" ); } elseif($save){ print( "<p align=\"center\">\n" ); print( "File: <b>" ); print( "$file" ); if($worked){print( "</b> has been saved!" );} else {print( "</b> has <b>not</b> been saved!" );} print( "</p>" ); } else { print( "<p align=\"center\"><b>Please Select A File To Be Save!</b></p>" ); } ?> <!-- End Of Page Content --> <BR><BR><BR> <div class="copyright">&copy;<a href="mailto:webmaster@corin.org.uk">Corin Rathbone</a> 2001</div> </body> </html>

« previous php.general (#58480) next »