Search code examples
phpfopenremoving-whitespace

Prevent fopen() from adding whitespace at the beginning of the file


Building a simple random winner picker with PHP. The winners are read from a text file and can be added manually to that file line by line. However, when I try openning the file for edition, there is a weird whitespace at the beginning and the end of the file. How can I prevent that from happenning?

Reading code is as follow:

<?php
$handle = fopen("list.txt", "r") or die("Unable to open file");
$count = 0;
if ($handle) {
while (($buffer = fgets($handle, 4096)) !== false) {
echo $buffer;
}
if (!feof($handle)) {
echo "Error: unexpected fgets() fail\n";
}
fclose($handle);

Writting code:

<?php
if ($_POST['submit']) {
$participants = explode(PHP_EOL, $_POST['participants']);
$participant_file = fopen("list.txt", "w") or die("Unable to open file!");
foreach ($participants as $participant) {
fwrite($participant_file, $participant . "\n");
}
fclose($participant_file);

Tried changing mode does'nt help, reading with "a" duplicates contents.

Edit: add xxd output:

00000000: 5061 7274 6963 6970 616e 7420 410a 5061  Participant A.Pa
00000010: 7274 6963 6970 616e 7420 420a 5061 7274  rticipant B.Part
00000020: 6963 6970 616e 7420 430a 5061 7274 6963  icipant C.Partic
00000030: 6970 616e 7420 440a 5061 7274 6963 6970  ipant D.Particip
00000040: 616e 7420 450a 5061 7274 6963 6970 616e  ant E.Participan
00000050: 7420 460a 5061 7274 6963 6970 616e 7420  t F.Participant 
00000060: 470a 5061 7274 6963 6970 616e 7420 480a  G.Participant H.
00000070: 5061 7274 6963 6970 616e 7420 490a 5061  Participant I.Pa
00000080: 7274 6963 6970 616e 7420 4a0a 5061 7274  rticipant J.Part
00000090: 6963 6970 616e 7420 4b0a 5061 7274 6963  icipant K.Partic
000000a0: 6970 616e 7420 4c0a 5061 7274 6963 6970  ipant L.Particip
000000b0: 616e 7420 4d0a 5061 7274 6963 6970 616e  ant M.Participan
000000c0: 7420 4e0a 5061 7274 6963 6970 616e 7420  t N.Participant 
000000d0: 4f0a 5061 7274 6963 6970 616e 7420 500a  O.Participant P.
000000e0: 5061 7274 6963 6970 616e 7420 510a 5061  Participant Q.Pa
000000f0: 7274 6963 6970 616e 7420 520a 5061 7274  rticipant R.Part
00000100: 6963 6970 616e 7420 530a 5061 7274 6963  icipant S.Partic
00000110: 6970 616e 7420 540a 5061 7274 6963 6970  ipant T.Particip
00000120: 616e 7420 550a 5061 7274 6963 6970 616e  ant U.Participan
00000130: 7420 560a 5061 7274 6963 6970 616e 7420  t V.Participant 
00000140: 570a 5061 7274 6963 6970 616e 7420 580a  W.Participant X.
00000150: 5061 7274 6963 6970 616e 7420 590a 5061  Participant Y.Pa
00000160: 7274 6963 6970 616e 7420 5a0a 5061 7274  rticipant Z.Part
00000170: 6963 6970 616e 7420 4141 0a50 6172 7469  icipant AA.Parti
00000180: 6369 7061 6e74 2041 420a 5061 7274 6963  cipant AB.Partic
00000190: 6970 616e 7420 4143 0a50 6172 7469 6369  ipant AC.Partici
000001a0: 7061 6e74 2041 440a 5061 7274 6963 6970  pant AD.Particip
000001b0: 616e 7420 4145 0a50 6172 7469 6369 7061  ant AE.Participa
000001c0: 6e74 2041 460a 5061 7274 6963 6970 616e  nt AF.Participan
000001d0: 7420 4147 0a50 6172 7469 6369 7061 6e74  t AG.Participant
000001e0: 2041 480a 5061 7274 6963 6970 616e 7420   AH.Participant 
000001f0: 4149 0a50 6172 7469 6369 7061 6e74 2041  AI.Participant A
00000200: 4a0a 5061 7274 6963 6970 616e 7420 414b  J.Participant AK
00000210: 0a50 6172 7469 6369 7061 6e74 2041 4c0a  .Participant AL.
00000220: 5061 7274 6963 6970 616e 7420 414d       Participant AM

enter image description here

Edit 2: The code that produces the above screenshot:

<textarea class="form-control" rows="15" name="participants" id="participants">
<?php 
if ($handle) {
while (($buffer = fgets($handle, 4096)) !== false) {
echo $buffer;
}
if (!feof($handle)) {
echo "Error: unexpected fgets() fail\n";
}
fclose($handle);
}
?>
</textarea>

Solution

  • The <?php tag is in the next line after <textarea>. That is the newline. Depending on the OS you will then get \n (mostly *nix/MacOs) or even \r\n (mostly Windows).

    The solution is to place the PHP open tag directly after the textarea tag in the same line.

    <textarea class="form-control" rows="15" name="participants" id="participants"><?php 
    if ($handle) {