Matthew Williams
Matthew Williams

Reputation: 101

Overwrite file in php issue

I am running a script which will do various things. One such thing is to produce the content of a css file to the screen, inside a html textarea box, and allow for editing and submission. In order to overwrite the existing file with the new content I use:

$myFile = "default.css";
$fh = fopen($myFile, 'w');
$stringData = $_POST['css'];
$write = fwrite($fh, $stringData);
fclose($fh);

If I add to the file and submit the content will erase and write to the file as expected. If, however, I remove content from the file the newly submitted data is appended to the top of the existing data rather than erase over it.

How am I able to make the file overwrite when data is both added and removed?

Update

To clarify, by added and removed, taking a small piece of my css file:

p, h1 {
text-align: center;
}

If I was to add color: blue; and submit, it would add this content and the rest of the file would remain as is. If I then remove this newly added information and submit again the new sheet (without color: blue;) would append to the top (Sorry, I first said bottom) so the sheet without color will be top and the entire sheet with color will be underneath.

I have already tried file_put_contents("default.css", $_POST['css']); with the same result. Including var_dump($stringData); shows a string length of 1500(ish) upon first submit and roughly 3000 upon second. As suggested I added $stringData; to clear the string once content is submitted for writing.

Since I don't want to leave bits out since the probably is likely glaringly obvious to someone here the full php making up this request ($data is to collect and print content to textarea):

if  (isset($_POST['css'])) {
    $myFile = "default.css";
    $fh = fopen($myFile, 'w');
    $stringData = $_POST['css'];
    $write = fwrite($fh, $stringData);
    var_dump($stringData);
    fclose($fh);
    $stringData;
                }

    $data = file_get_contents("default.css");

The form which controls this:

    <form onsubmit="return confirm('Confirm Submission');" method="post" action="<?php echo ($_SERVER["PHP_SELF"]);?>">
    <textarea cols="60" rows="20" name="css" id="css"><?php echo $data; if(isset($_POST['css'])) {  
         echo htmlentities ($_POST['css']); }?>
    </textarea>
<br><br>
    <input type="submit" value="Submit CSS"/>
    </form>

Upvotes: 0

Views: 527

Answers (4)

Matthew Williams
Matthew Williams

Reputation: 101

After trying suggestions and trying a couple of my own I made a rather annoying discovery [in that I didn't spot it right away]. I discovered the duplication was coming from the textarea box, as suggested by @Mr. Helper. Each time data was submitted it was being appended to the end of the box.

In order to solve the problem I moved $data = file_get_contents("default.css"); variable to a separate PHP file (style.php) which includes a $data; clear aspect just in case. The style.php file is run through an onclick ajax request. Now when the submit button is pressed there is no request to print the existing css sheet to the text area from the $data variable.

Thanks for all the suggestions. I have picked up some better methods of implementing my code from all the comments and answers.

Upvotes: 0

gladiola
gladiola

Reputation: 133

I suspect the trouble lies in $stringData. Try var_dump($stringData) somewhere on the page. See if the new, reduced, data is getting appended. Does $stringData need to be cleared out? $stringData; would do it.

w in file ops will generally write fresh. a will append. I suspect the trouble is with the form processing and not with the file ops.

Upvotes: 3

Wogan
Wogan

Reputation: 1335

That doesn't really make sense - opening with mode w should write, and puts the pointer at the beginning of the file. Try w+ instead of w, and if that doesn't work, just use file_put_contents. That turns the entire code block above to:

file_put_contents("default.css", $_POST['css']);

Upvotes: 1

iamdev
iamdev

Reputation: 726

Just use file_put_contents() and you'll have no trouble. That function takes a string, and writes to a file (without appending).

And all in one line!

file_put_contents("default.css", $_POST['css']);

Upvotes: 1

Related Questions