Jump to content

fgets not working?


abazoskib

Recommended Posts

here's some code Ive used a million times before that isn't working for some reason. i am trying to parse a text file into a database however for some reason i can only read the first line of each file

 

foreach($files as $file) {
        $fh = fopen($file, 'r') or die("Can't open file");
        while(!feof($fh)){
                $line = fgets($fh);
                $pieces = explode(",",$line); echo $pieces[0]."\n";
                $email = $pieces[0];
                //if ($email != "Email") {
                        //$query = "INSERT INTO unsubscribe(email,timestamp) VALUES('$email',NOW())";
                        //$result = mysql_query($query);
                //}
        }
        fclose($fh);
        //$newPath = $dir."BACKUPS/".$file;
        //rename($fileName, $newPath);

}

 

i get the email addresses from the first line of each file. is my code screwed up or could it be the file?

Link to comment
Share on other sites

The only ways the posted code would get only one line from each file is if the new-line character(s) are not what php expects or there is only one line in each file (or the rest of the code that you did not post is truncating the files after the first line...)

 

You are not showing sufficient information about what your data, code, and results are for us to guess why it is not working.

 

Edit: and the only way the print_r(file()) code would only show one line is if the file only has one line in it at the time the print_r() is being executed.

Link to comment
Share on other sites

It is not possible for file($file) to only grab one line of the file.  file grabs all of the contents of the file, putting each line into an array element (\n) being the delimiter).  Even if the \n's were whack, the entire file would still be in there somewhere, even if it's just in the 1st array element.  So if print_r(file($file)) is only returning "1 line" the only way that is possible is that the file only contains 1 line.  Are you pointing to the right file? File contain what you expect?

Link to comment
Share on other sites

ok here's some new info. the files contain around 200 lines each(5 files) of comma delimited data. however, when i open the files in nano, the bottom info footer says 'converted from mac format'..could this be an issue?

 

i am in fact only getting one line per file when i print_r.

Link to comment
Share on other sites

the extension was .csv but should still be plain text..anyways i tried copy pasting everything into a new text file and still doesnt work. i think there is something wrong with the file. what do mas use as carriage return?

 

btw: the script works with other files. fgets isnt compatible with mac formatted text?

Link to comment
Share on other sites

Based on the symptoms, the file(s) has a EOF (End Of File) character in it that is stopping it from being read. Both file() and fgets() is stopping after one line.

 

The new-line character is either just a \r, a \n, or a \r\n or a \n\r. However, if this is the problem, it would cause all the content to appear to be on one line and the print_r(file()) would have still shown all the content (did you do a "view source" in your browser just to check what exactly was present from the print_r(file())?)

 

Do you actually have just one file or more? I suspect code before the foreach() loop could be causing this. What is your actual code?

Link to comment
Share on other sites

the extension was .csv but should still be plain text..anyways i tried copy pasting everything into a new text file and still doesnt work. i think there is something wrong with the file. what do mas use as carriage return?

 

btw: the script works with other files. fgets isnt compatible with mac formatted text?

 

Open the file in an hex editor and post the hex that end each line.

Link to comment
Share on other sites

While typing my next question, I just realised your problem.

 

Sometimes when I download a file and open it using notepad.exe everything is on the first line. However when using wordpad.exe it display's just fine. So possibly you are using an editor that uses an unix line-delimiter. You are opening it using php (under windows?) which uses the windows line-delimiter (PHP_EOL). You read the first line, pop off the e-mail address and end up with only one e-mail address.

 

Open the file using notepad and see if it is all on one-line. If it is open your editor and change it's settings to use the windows line-delimiter.

 

opened it in a hex editor but saw nothing that marked the end of a line..

 

0D0A

 

EOF:

 

1A

Link to comment
Share on other sites

I am only running linux, however now that I used your hex example...in the "bad" file I noticed that it was

0d

that ended the file and

0a

that ended the good files(from previous transactions).

 

What's the interpretation of that?

Link to comment
Share on other sites

I am only running linux, however now that I used your hex example...in the "bad" file I noticed that it was

0d

that ended the file and

0a

that ended the good files(from previous transactions).

 

What's the interpretation of that?

 

0D=\r

0A=\n

 

On windows this typically is: 0D0A

Link to comment
Share on other sites

tried doing this to convert..didn't work. it cant be this hard to fix a carriage return problem.

 

foreach($files as $file) {
        $ext = substr(strrchr(strtolower($file), "."), 1);
        if(($file != ".") && ($file != "..") && (($ext == "csv"))){
                $data = file_get_contents($file);
                $search = "chr(13)";
                $replace = "chr(10)";
                $data = str_replace($search,$replace,$data);
                $fh = fopen($file, 'w') or die("Can't open file");
                fwrite($fh, $data);
                fclose($fh);

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.