Hi all,
I have a php script that writes to a file but I am having some problems with it. The file that I am writing is highly sensitive YML file and it breaks every time I edit it using my web editor. What happens is the script seemingly appends a space on the first line every time the file is opened and then saved. I have tried changing the fopen mode.
Any help greatly appreciated:
PHP Code:
$filename = "../config.yml";
$newdata = $_POST['newd'];
if(isset($_POST['submit1']))
{
header("location:config.php");
}
if ($newdata != '')
{
// open file
$fw = fopen($filename, 'w') or die('Insufficent permissions to open file with write access!');
// write to file
// added stripslashes to $newdata
$fb = fwrite($fw,stripslashes($newdata)) or die('Could not write to file');
// close file
fclose($fw);
}
$fh = fopen($filename, "r") or die("Could not open file!");
$data = fread($fh, filesize($filename)) or die("Could not read file!");
fclose($fh);
echo "<h3>Contents of File</h3> ";
echo "<form method=\"post\" action=\"$_SERVER[php_self]\">";
echo "<input type=\"submit\" value=\"Change\"> ";
echo "<input type=\"hidden\" name=\"cancel\" value=\"cancel\">";
echo "<input type=\"submit\" name=\"submit1\"value=\"Cancel\">";
echo "<textarea name='newd' cols='100%' rows='50'> $data </textarea> ";
echo "<input type='submit' value='Change'> ";
echo "<input type=\"hidden\" name=\"cancel\" value=\"cancel\">";
echo "<input type=\"submit\" name=\"submit1\"value=\"Cancel\">";
echo "</form>";
Thanks in advance.
Bookmarks