Hi,

I have a snippet code below

<?php

	//get value of $new from other codes
        //$new will provide outputs of a new number every time it is executed  
	
        echo "$new<br/>";
	$strFile = "1.txt";
	file_put_contents($strFile, $new);

The idea is that I want to put a new line of number in 1.txt every time this code is executed.
So basically, I wanted to have ("number" below will be some integer number ) :


"number1"
"number1,number2"
"number1,number2,number3"

example :
1
1,7
1,7,10
and so on..

inside the file of 1.txt.

How can I fix the code above so that it will provide me with the output that I wanted?

I am sorry for my bad English, but I am thank you for the suggestion

Hi,

Instead of using a <br /> tag, try using:

$strNew = "This is my string\r\n";

Also, I notice that you're overwritting the entire file content during each iteration of your code. If you want to add another line, you'll need to read the content from the file first, using the file_get_contents() function, and write the existing content together with the new content to the file again. E.g:

$strFilePath = '/path/to/file.txt';
$strFileContents = '';

// Check that file exists and is readable
if(true === file_exists($strFilePath) && true === is_readable($strFilePath)) {
    $strFileContents = file_get_contents($strFilePath);
}

$strFileContents .= "$strNewContent\r\n";
file_put_contents($strFilePath, $strFileContents);

Regards,

Rob.

aha!
thanks for giving me inspiration.. !
i implement your idea in my source code, and it works like charm!
(it took me sometime to understand it, that is why i did not response directly)

thanks Rob!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.