1

I have three HTML form field values (name, saywords, mail) that I try to concat in php and write into one single txt file on my server:

Each value should be on a new line in the txt field, as is why I added the "\n" in the array .... where's my error?

Thanks a lot for any help!

$name = $_POST['name'];
$saywords = $_POST['saywords'];
$mail = $_POST['mail'];

$data = array($name,  . "\n" $mail, . "\n" $saywords);

file_put_contents("$t.$ip.txt",$data); // Will put the text to file
3
  • file_put_contents($file, implode(‘\n’, $data)) and remove new lines from the array Commented Mar 17, 2018 at 20:25
  • 2
    Why are you creating an array? Just do $data = $name . "\n" . $mail . "\n" . $saywords; Commented Mar 17, 2018 at 20:25
  • Perfect! Thanks a lot!! :) Commented Mar 17, 2018 at 20:32

5 Answers 5

3

You have two problems:

  • $data should be a string not an array
  • . concatenates the left hand side and the right hand side: "abc" . "def" becomes "abcdef".
    • putting the dot first like in . "\n" or even . "\n" $mail doesn't make sense in PHP so you'll get a parse error.

Replace your $data = line with $data = $name . "\n" . $mail . "\n" . $saywords; and you'll be good to go.

Sign up to request clarification or add additional context in comments.

1 Comment

Perfect! Thanks a lot!!
3

i don't see the use of array , you can concatenate them just like :

$data = $name . "\n" . $mail . "\n" . $saywords ;

1 Comment

Perfect! Thanks a lot!!
1

It depends with the operating system of the server. Try "\r\n" instead of "\n"

1 Comment

If you look at the OP's code, there are some bigger issues.
0

Okay, so here is my shot.

/*first of all, you should always check if posted vars are actually set
and not empty. for that, you can use an universal function "empty", which
checks if the variable is set / not null / not an empty string / not 0. 
In such way you will avoid PHP warnings, when some of these variables 
will not be set*/

        $name = !empty($_POST['name']) ? $_POST['name'] : '';
        $saywords = !empty($_POST['saywords']) ? : $_POST['saywords'] : '';;
        $mail = !empty($_POST['mail']) ? $_POST['mail'] : '';

/*Secondly, do not use \n, \r, \r\n, because these are platform specific. 
Use PHP_EOL constant, it will do the job perfectly, by choosing 
what type of line-break to use best.

    As others mentioned - in your scenario, the string would be better solution. 
Add everything into string, and then put its contents into file. Avoid using 
double quotes, when you define PHP strings, and use single quotes instead - for 
performance and cleaner code.

    */


        $data = 'Name: '.$name.PHP_EOL.'E-Mail: '.$mail.PHP_EOL.'Message: '.$saywords.PHP_EOL.PHP_EOL;


        file_put_contents($t.$ip.'.txt', $data); // Will put the text to file

By the way, I strongly suggest to also add some extra validation, before saving data to that txt file. With this code somebody can easily mess up contents of your txt file, by posting huge amounts of data with no limits.

Tips:

1) Accept only Names with limited lengths and charaters (do not allow to use special symbols or line breaks - you can also filter them out, before saving)

2) Validate e-mail which has been entered - if it is in correct format, does mx records exists for the domain of e-mail address, and so on...

3) Accept "saywords" with limited length, and if needed - deny or filter out special characters.

You will get much cleaner submissions by doing this way.

Comments

-1

Use <br /> as you use html code

1 Comment

The OP is saving the data in a text file, not outputting it, so \n is correct (not my down vote, btw).

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.