Search code examples
phpquotesfwrite

Php fwrite not working when content has quotes


Supposing i have this kind of text passing through a form:

Hello my name is Mike, friends call me "SuperMike", hello 'All'!

As you can see , the text contains both type of quotes ( ' " ) and when i try to write a file, it just goes wrong (blank file).

$text = $_POST['text'];
    $myfile = fopen("text.html", "w");
    fwrite($myfile, $text);
    fclose($myfile); 

Solution

  • As per my understanding ,below code will help you

    <form method="post">
        <textarea name="valll"></textarea>
        <input type="submit" name="ff">
    </form>
    <?php
    if(isset($_POST['ff'])) {
        $text = base64_encode($_POST['valll']);
        /*echo $_POST['valll'];
        exit;*/
        $myfile = fopen("text.html", "w");
        fwrite($myfile, base64_decode($text));
        fclose($myfile);
    }
    
    ?>