Forum Moderators: coopster
Is this indeed the case? The array was generated with the file function, and I used str_replace to remove all occurrences of chr(10) and chr(13).
How do you write an array to a file with fopen, when fopen will only accept a string as the second argument?
I used the following script to test fwrite:
$a = array('Aaron','Carter');
$x = fopen("upload_dir/testfile.txt", "w");
fwrite($x, implode('',$a));
fclose($x);
produces a file whose content looks like
AaronCarter
There is no newline anywhere.
How do you write an array to a file with fopen, when fopen will only accept a string as the second argument?
Well, I read the file into an array with file (). Then I remove all newline characters with a str_replace over all elements of the array (removing all occurrences of chr(10) and chr(13)).
Then I write one by one all elements of the array to a new file with a for loop. In the resulting file there are newlines at the end of each array element. PHP must be adding the newlines automatically.