Forum Moderators: coopster

Message Too Old, No Replies

Problem with editing file

         

KrazyKid

1:49 am on Jul 13, 2005 (gmt 0)

10+ Year Member



What I have is a script that allows authorized users to edit a .inc file, which is then displayed on my home page. But occasionally when I try to edit the file using this script, everything in the file gets erased (this seems to happen randomly). Here is the page with the form used to edit the file:

<?php
$pass = $_GET['pass'];
if($pass == 'beatles2') {
echo "To ad an event:<br>
1) Copy this code: <br><textarea><br>1/1/05
<p>TYPE EVENTS HERE</p></textarea><br>
2)Put your cursor in the top, left corner of the text box below. Hit enter twice.<br>
3)Paste the code into the top of the textbox below.<br>
4)Fill in the correct date and events, and press the Edit button.<br><form name='editNews' method='post' action='http://www.example.com/skateboarders/write_to_news.php'>
<textarea name='text' cols='50' rows='10'>";
include "happenings.inc";
echo "</textarea><br><input type='submit' value='Edit' name='submit'></form>";
} else {
echo "You don't have sufficient priveliges to access this page!";
}
?>

And here is the script that the changes get sent to. This script is what actually does the editing of the file:

<?php
$somecontent = $_POST['text'];
$filename = 'happenings.inc';
$somecontent = str_replace("'", "\'", $somecontent);
$somecontent = str_replace('"', "'+String.fromCharCode(34)+'", $somecontent);

// Let's make sure the file exists and is writable first.
if (is_writable($filename)) {

// In our example we're opening $filename in append mode.
// The file pointer is at the bottom of the file hence
// that's where $somecontent will go when we fwrite() it.
if (!$handle = fopen($filename, 'w')) {
echo "Cannot open file ($filename)";
exit;
}

// Write $somecontent to our opened file.
if (fwrite($handle, $somecontent) === FALSE) {
echo "Cannot write to file ($filename)";
exit;
}

echo "Success, edited ($filename)<br><a href='index.php?pw=beatles'>Back to Home</a> or <a href='editnews.php?pass=beatles2'>Edit more</a>";

fclose($handle);

} else {
echo "The file $filename is not writable";
}
?>

I know that the security isn't worth anything, but I'm not worried about that right now, I'll fix that later. I just need to know why the content of happenings.inc is getting erased randomly. Thanks!

[edited by: coopster at 10:41 am (utc) on July 13, 2005]
[edit reason] generalized url [/edit]

jatar_k

5:04 pm on Jul 13, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



my wild guess is that it isn't getting the content written or the var that contains the content is empty so it actually write nothing to the file.

I think you will have to test a little to isolate the case more but here's my logic

w mode with fopen

Open for writing only; place the file pointer at the beginning of the file and truncate the file to zero length. If the file does not exist, attempt to create it.

so once opened the file will be blank
if there is no error, that worked
if you don't get an error msg on fwrite then that worked

so..you should probably look to the $somecontent var first, echo it's value a few times as it is changed in the top few lines. Maybe $_POST['text'] is the problem, not sure, but that's where I'd start.