0

PHP

<?php
if ($changefile) {
    $savedEdit = stripslashes($_POST['filetest']);
    $filetochange = "../dashboard/list.ini";
    $filetochangeOpen = fopen($filetochange,"w") or die ("Error editing.");
    fputs($filetochangeOpen,$savedEdit);
    fclose($filetochangeOpen) or die ("Error Closing File!");
}
?>

HTML form

<form method=post action="mmtst.php">   
    <textarea rows="40" cols="60" name="filetest">
    <?
        // Implode CSS
        $filetochange = "../dashboard/list.ini";
        print (implode("",file($filetochange)));
    ?>
    </textarea><br/><br/>
    <input type="submit" value="Save Changes" name="changefile">
</form>

Here I have a page that displays an INI file in a text box. What should happen is that when I click the submit button, the original file is opened, and fputs writes to the original with anything that is displayed in the txtbox. (the original file is set to permissions 777). However, nothing happens when the submit button is clicked, I have no idea what I am doing wrong, any suggestions would be awesome.

1
  • 1
    all write-related code is in a branch: if($changefile), try dumping that, because by the looks of things: it might be an undefined variable, in which case the file is never opened, let alone written to. Also checkout file_put_contents, replace the put with get to get it's reading counterpart. It doesn't require those extra lines of opening and closing a file Commented Dec 17, 2012 at 19:40

2 Answers 2

1

The issue is simple, you have register_globals disabled and you are trying to use it, this setting allow PHP to automatically define variables depending on received parameters from GET and POST data, but it's considered insecure and has been deprecated.

To access the values passed in a post for use the $_POST variable, for get use the $_GET

if( isset( $_POST["changefile"] ) ) {

In case you just want to check if a post form as been submitted you could !empty($_POST) and leave the submit button without name:

<?php //...
if( !empty($_POST) ) {
//...
?>
<input type="submit" value="Save Changes">
Sign up to request clarification or add additional context in comments.

Comments

1

You are checking for a

if ($changefile) {

You need to check for the $_POST version of the variable, the same you do for "filetest" variable, so you should for example...

if (isset($_POST["changefile"])) {

Otherwise the condition never returns true, and the code block doesn't get executed

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.