I have a text file that I create on a windows systems that I then upload into a MySQL database (also running on Windows) using LOAD DATA LOCAL INFILE. When I first tried this, only every other record loaded, and I assumed that this was because in Windows, text file lines are termineated with a Carriage Retern followed by a Line Feed (ascii 10 13). I wrote a PHP script to strip off these two ascii characters and then add back on just the line feed. My PHP script looks litke this.
<?php
if (!$fd1 = fopen("file.txt","r")) {
echo("Could not open uploaded file");
} else {
$fd2 = fopen("c:\mysql\bin\file2.txt", "w");
echo("Reformat started<BR>");
while (!feof($fd1)) {
$recordcount++;
$recbuffer = fgets($fd1, 4096);
$recbuffer = trim($recbuffer);
fputs($fd2,$recbuffer . chr(10));
}
echo("Reformat complete");
fclose($fd2);
fclose($fd1);
}
?>
When I first executed this, I SWEAR I looked at the file, and voila, the ascii 10 13 characters were gone and in their place was a single ascii 10. And the data loaded swimmingly. Now, a few days later, I needed to look at the data I was creating and I noticed that the end-of-line characters being generated were ascii 10 13 once again. Strangely enough, the file still loads. Everything still works fine. And stranger still, when I try and load a file that I have NOT run through my script, I get the every-other-record problem again. So, my script is producing the result I want, but it is either not doing it how I thought it was, or else my UltraEdit viewer is lying to me.
These are my questions: 1) Why does my file end up with ascii 10 13 when I am appending only ascii 10 and 2) is there something else going on here that would make my file load correctly after running my PHP script, even though it appears that I did not change the end-of-line character.