0

HTML Code:

 <form class="form-horizontal" action="<?php echo htmlspecialchars($_SERVER[" PHP_SELF "]); ?>" method="POST" enctype="multipart/form-data">
            <div class="form-group">
                <label class="col-sm-2 control-label">Write File</label>
                <div class="col-sm-6">
                    <input type="file" class="form-control-file" id="fileToWrite" name="fileToWrite">
                </div>
                <div class="col-sm-6">
                    <textarea type="text" class="form-control" rows="5" name="write_file"></textarea>
                </div>
                <button type="submit" class="col-sm-2 btn btn-default">Write</button>
            </div>
        </form>

PHP Code:

<?php
if ("" != trim($_FILES['fileToWrite']["tmp_name"])) {
    if (!file_exists($_FILES['fileToWrite']["tmp_name"])) {
        echo "File named: <span style='color:red;'>" . $_POST['fileToWrite'] . "</span> doesn't exist! <br>";
    } else {
        $fw = fopen($_FILES['fileToWrite']["tmp_name"], "w");
        fwrite($fw, $_POST['write_file']);
        echo 'File over-written successfully';
    }
}
?>

Output: File over-written successfully

Text File is stored at Desktop or any other place, php file is stored in htdocs. (It doesn't matter anyway)

I am taking input from user using textarea, and taking file to be edited using input type=file. After submitting, it shows successfull message, but doesn't make any change to file. Please help me out.

9gagger07
  • 11
  • 2
  • See http://stackoverflow.com/q/845021/101087 on how to enable output of errors and warnings. – NineBerry Feb 11 '17 at 18:03
  • @NineBerry Ok...enabled and no errors or warnings displayed. – 9gagger07 Feb 11 '17 at 18:06
  • One thing can be that you are not closing your file with `fclose`. Also have a look at http://php.net/manual/en/function.move-uploaded-file.php to move an uploaded file into a new location. – jkrnak Feb 11 '17 at 18:09
  • What file do you expect to change? The file that was selected in the HTML form? You cannot do that. What you are currently doing is writing into a copy of that file in your temp directory. – NineBerry Feb 11 '17 at 18:11
  • @NineBerry Ok. So what is the possible solution? I tried moving the file to a new location. It didn't made any difference. – 9gagger07 Feb 11 '17 at 18:18
  • You cannot write to a file on the client computer from a php script on a web server. – NineBerry Feb 11 '17 at 18:21
  • @NineBerry I am currenty using a localserver. Not really a webserver, just a personal PC with Xampp. – 9gagger07 Feb 11 '17 at 18:21
  • @NineBerry got the solution. – 9gagger07 Feb 11 '17 at 18:24
  • @jkrnak thanks for helping out, moving the file properly worked out. – 9gagger07 Feb 11 '17 at 18:25

0 Answers0