0

file_put_contents seems to truncate my string variable.

HTML

<div id="element">
    <?php echo file_get_contents("file.txt"); ?>
</div>

JavaScript

function submitData() {
    var string = $('textarea[name=string]').val();
    $('#element').load("submit.php?var="+string);
    $('textarea[name=string]').val("");
    var height = $('#element')[0].scrollHeight;
    $('#element').animate({
        scrollTop: height
    }, 1000);
    $('textarea[name=string]').focus();
}

PHP

$str = nl2br($_GET['var']);
if(empty($str)) {
    $str = "Error: Empty string.";
}
$str .= "<br /><br />";
$delete = $_GET['delete'];
if(isset($delete)) {
    file_put_contents("file.txt", "");
    echo file_get_contents("file.txt");
} else {
    file_put_contents("file.txt", $str.PHP_EOL, FILE_APPEND);
    echo file_get_contents("file.txt");
}

Result

If the string is "Hello, World!"; I get the following result:

Hello,
JustSomeGuy
  • 315
  • 2
  • 17
  • You will need to URL encode your string if you're passing over GET, eg `load("submit.php?var=" . some_function_to_url_encode(string))` I don't use jQuery, so I can't speak to the proper method to URL encode. – bishop Jan 18 '14 at 01:02

1 Answers1

2

You need to use URL encoded data when sending somthing with a GET request. see here for how to do it.

Community
  • 1
  • 1
B4dT0bi
  • 623
  • 4
  • 21