Jump to content

[SOLVED] PHP editor not working. Loads file, when I click save, it does not


daveoffy

Recommended Posts

I have this text editor for my website and is in PHP. It loads the file, but when I click save, it just goes to editor.php on my site and it doesn't save. It loads everything fine.

 

$loadcontent = 'sites/'.$username.'/'.$sitename.'/'.$file;
    if($save_file) {
        $savecontent = stripslashes($savecontent);
        $fp = @fopen($loadcontent, "w");
        if ($fp) {
            fwrite($fp, $savecontent);
            fclose($fp);
                               }
                }
    $fp = @fopen($loadcontent, "r");
        $loadcontent = fread($fp, filesize($loadcontent));
        $loadcontent = htmlspecialchars($loadcontent);
        fclose($fp);
}
?><form method=post action="<?=$_SERVER['PHP_SELF']?>">
<textarea name="savecontent" class="editor_box"><?=$loadcontent?></textarea>
<br>
<div style="float: right;"><input type="submit" name="save_file" value="Save"></div>
</form>

Your code is will not work unless register_globals is enabled, this setting is now disabled as default (as from 2002) and is soon to be removed from the upcoming release of PHP6.

 

To fix you script

$save_file should be $_POST['save_file'];

$savecontent should be $_POST['savecontent'];

 

Where is $username and $sitename coming from?

$username, $site, $file and all are above in the code.

 

this code does not work, does same thing

 

//Start loading file
$loadcontent = 'sites/'.$username.'/'.$sitename.'/'.$file;
    if($_POST['save_file']) {
        $savecontent = stripslashes($_POST['savecontent']);
        $fp = @fopen($loadcontent, "w");
        if ($fp) {
            fwrite($fp, $_POST['savecontent']);
            fclose($fp);
                               }
                }
    $fp = @fopen($loadcontent, "r");
        $loadcontent = fread($fp, filesize($loadcontent));
        $loadcontent = htmlspecialchars($loadcontent);
        fclose($fp);
}
?><form method=post action="<?=$_SERVER['PHP_SELF']?>">
<textarea name="savecontent" class="editor_box"><?=$loadcontent?></textarea>
<br>
<div style="float: right;"><input type="submit" name="save_file" value="Save"></div>
</form>

$username, $site, $file and all are above in the code.

I see they are used on this line

$loadcontent = 'sites/'.$username.'/'.$sitename.'/'.$file;

 

But where are they defined/coming from? Variables must be defined before using them.

 

Also you should avoid using short tags  always use the full PHP syntax for tags (<?php ?>) and not <? ?>. The full syntax for <?= ?> is <?php echo ?>

I  have this, don't get any errors, it loads fine when at the url editor.php?fileid=1&site=1, than when I press save it just goes to editor.php and it doesn't save the work.

 

THERE IS CODE UP HERE and I do start a brace so don't say remove the last brace. 
<?php
$loadcontent = 'sites/'.$username.'/'.$sitename.'/'.$file;
    if($save_file) {
        $savecontent = stripslashes($savecontent);
        $fp = @fopen($loadcontent, "w");
        if ($fp) {
            fwrite($fp, $savecontent);
            fclose($fp);
                               }
                }
    $fp = @fopen($loadcontent, "r");
        $loadcontent = fread($fp, filesize($loadcontent));
        $loadcontent = htmlspecialchars($loadcontent);
        fclose($fp);
}
?><div align="center"><form method=post action="<?php echo $_SERVER['PHP_SELF'] ?>">
<textarea name="savecontent" class="editor_box"><?php echo $loadcontent ?></textarea></div>
<br>
<div style="float: right;"><input type="submit" name="save_file" value="Save"></div>
</form>

I  have this, don't get any errors, it loads fine when at the url editor.php?fileid=1&site=1, than when I press save it just goes to editor.php and it doesn't save the work.

 

 

Cause you are suppressing them!

 

remove @ from any function

I  have this, don't get any errors, it loads fine when at the url editor.php?fileid=1&site=1, than when I press save it just goes to editor.php and it doesn't save the work.

 

THERE IS CODE UP HERE and I do start a brace so don't say remove the last brace. 
<?php
$loadcontent = 'sites/'.$username.'/'.$sitename.'/'.$file;
    if($save_file) {
        $savecontent = stripslashes($savecontent);
        $fp = @fopen($loadcontent, "w");
        if ($fp) {
            fwrite($fp, $savecontent);
            fclose($fp);
                               }
                }
    $fp = @fopen($loadcontent, "r");
        $loadcontent = fread($fp, filesize($loadcontent));
        $loadcontent = htmlspecialchars($loadcontent);
        fclose($fp);
}
?><div align="center"><form method=post action="<?php echo $_SERVER['PHP_SELF'] ?>">
<textarea name="savecontent" class="editor_box"><?php echo $loadcontent ?></textarea></div>
<br>
<div style="float: right;"><input type="submit" name="save_file" value="Save"></div>
</form>

Post your code in full.

<?php
if(isset($_GET['fileid'], $_GET['site']) && is_numeric($_GET['fileid']) && is_numeric($_GET['site']))
{
    $fileid = $_GET['fileid'];
    $siteid = $_GET['site'];
$username = $_COOKIE['username'];
$user_id = "SELECT * FROM users WHERE username = '$username'";
$user_id_result = mysql_query($user_id);
while($user_row = mysql_fetch_array($user_id_result))
$userid = $user_row['id'];
$findsite = "SELECT * FROM site WHERE site_id = '$siteid'";
$find_result = mysql_query($findsite);
while($find_row = mysql_fetch_array($find_result))
$sitename = $find_row['site'];
$findfile = "SELECT * FROM file WHERE fileid = '$fileid'";
$find_file_result = mysql_query($findfile);
while($findfile_row = mysql_fetch_array($find_file_result))
$file = $findfile_row['filename'];
//Start loading file
$loadcontent = 'sites/'.$username.'/'.$sitename.'/'.$file;
    if($save_file) {
        $savecontent = stripslashes($savecontent);
        $fp = fopen($loadcontent, "w");
        if ($fp) {
            fwrite($fp, $savecontent);
            fclose($fp);
                               }
                }
    $fp = fopen($loadcontent, "r");
        $loadcontent = fread($fp, filesize($loadcontent));
        $loadcontent = htmlspecialchars($loadcontent);
        fclose($fp);
}
?><div align="center"><form method=post action="<?php echo $_SERVER['PHP_SELF'] ?>">
<textarea name="savecontent" class="editor_box"><?php echo $loadcontent ?></textarea></div>
<br>
<div style="float: right;"><input type="submit" name="save_file" value="Save"></div>
</form>

Wildteen88 already told you why the logic in your code is not evaluating as true and executing -

Your code is will not work unless register_globals is enabled, this setting is now disabled as default (as from 2002) and is soon to be removed from the upcoming release of PHP6.

 

To fix you script

$save_file should be $_POST['save_file'];

$savecontent should be $_POST['savecontent'];

Cleaned your script up try

<?php

if(isset($_GET['fileid'], $_GET['site']) && is_numeric($_GET['fileid']) && is_numeric($_GET['site']))
{
    $fileid = $_GET['fileid'];
    $siteid = $_GET['site'];
    $username = $_COOKIE['username'];

    $user_id = "SELECT id FROM users WHERE username = '$username'";
    $user_id_result = mysql_query($user_id);
    list($userid) = mysql_fetch_row($user_id_result);

    $findsite = "SELECT site FROM site WHERE site_id = '$siteid'";
    $find_result = mysql_query($findsite);
    list($sitename) = mysql_fetch_row($find_result);

    $findfile = "SELECT filename FROM file WHERE fileid = '$fileid'";
    $find_file_result = mysql_query($findfile);
    list($file) = mysql_fetch_row($find_file_result);

    $loadcontent = 'sites/'.$username.'/'.$sitename.'/'.$file;

    //Start loading file
    if(isset($_POST['save_file']))
    {
        echo 'EDITING '.$loadcontent.'<br />';

        echo 'READABLE: '.(is_readable($loadcontent) ? 'true' : 'false') . '<br />';
        echo 'WRITABLE: '.(is_writable($loadcontent) ? 'true' : 'false') . '<br />';

        $savecontent = stripslashes($_POST['savecontent']);

        if ($fp = fopen($loadcontent, "w"))
        {
            echo 'OPENED: '.$loadcontent.'<br />';
            fwrite($fp, $savecontent);
            fclose($fp);
            echo 'CLOSED';
        }
        else
        {
            echo 'Could not save changed to '.$loadcontent;
        }
    }

    $loadcontent = htmlspecialchars( file_get_contents($loadcontent) );
}

?>

It should output some debug messages to see whats going on

Woops I missed of your form. Sorry about that.

<?php

if(isset($_GET['fileid'], $_GET['site']) && is_numeric($_GET['fileid']) && is_numeric($_GET['site']))
{
    $fileid = $_GET['fileid'];
    $siteid = $_GET['site'];
    $username = $_COOKIE['username'];

    $user_id = "SELECT id FROM users WHERE username = '$username'";
    $user_id_result = mysql_query($user_id);
    list($userid) = mysql_fetch_row($user_id_result);

    $findsite = "SELECT site FROM site WHERE site_id = '$siteid'";
    $find_result = mysql_query($findsite);
    list($sitename) = mysql_fetch_row($find_result);

    $findfile = "SELECT filename FROM file WHERE fileid = '$fileid'";
    $find_file_result = mysql_query($findfile);
    list($file) = mysql_fetch_row($find_file_result);

    $loadcontent = 'sites/'.$username.'/'.$sitename.'/'.$file;

    //Start loading file
    if(isset($_POST['save_file']))
    {
        echo 'EDITING '.$loadcontent.'<br />';

        echo 'READABLE: '.(is_readable($loadcontent) ? 'true' : 'false') . '<br />';
        echo 'WRITABLE: '.(is_writable($loadcontent) ? 'true' : 'false') . '<br />';

        $savecontent = stripslashes($_POST['savecontent']);

        if ($fp = fopen($loadcontent, "w"))
        {
            echo 'OPENED: '.$loadcontent.'<br />';
            fwrite($fp, $savecontent);
            fclose($fp);
            echo 'CLOSED';
        }
        else
        {
            echo 'Could not save changed to '.$loadcontent;
        }
    }

    $loadcontent = htmlspecialchars( file_get_contents($loadcontent) );
}

?>

<div align="center"><form method=post action="<?php echo $_SERVER['PHP_SELF'] ?>">
<textarea name="savecontent" class="editor_box"><?php echo $loadcontent ?></textarea></div>
<br>
<div style="float: right;"><input type="submit" name="save_file" value="Save"></div>
</form>

Change

 

<div align="center"><form method=post action="<?php echo $_SERVER['PHP_SELF'] ?>">

 

to

 

<div align="center"><form method=post action="<?php echo $_SERVER['REQUEST_URI'] ?>">

 

The problem is when the form is submitted it is cleaning the fileid and site from the url.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.