abazoskib Posted July 25, 2009 Share Posted July 25, 2009 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? Quote Link to comment https://forums.phpfreaks.com/topic/167392-fgets-not-working/ Share on other sites More sharing options...
ignace Posted July 25, 2009 Share Posted July 25, 2009 i get the email addresses from the first line of each file. is my code screwed up or could it be the file? What does this give you? print_r(file($file)); Quote Link to comment https://forums.phpfreaks.com/topic/167392-fgets-not-working/#findComment-882625 Share on other sites More sharing options...
abazoskib Posted July 25, 2009 Author Share Posted July 25, 2009 yeah it definetly did not print out the whole file. would null values inbetween commas ie email,,,first_name,last_name cause that problem? Quote Link to comment https://forums.phpfreaks.com/topic/167392-fgets-not-working/#findComment-882637 Share on other sites More sharing options...
.josh Posted July 25, 2009 Share Posted July 25, 2009 null values would not cause that problem. Your script is only grabbing and using the first element (element 0) from the explode, so that doesn't matter. did print_r(file($file)); not print out the whole file? Quote Link to comment https://forums.phpfreaks.com/topic/167392-fgets-not-working/#findComment-882657 Share on other sites More sharing options...
abazoskib Posted July 25, 2009 Author Share Posted July 25, 2009 it did not print out the whole file. i believe it grabbed one line from each file. Quote Link to comment https://forums.phpfreaks.com/topic/167392-fgets-not-working/#findComment-882660 Share on other sites More sharing options...
PFMaBiSmAd Posted July 25, 2009 Share Posted July 25, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/167392-fgets-not-working/#findComment-882668 Share on other sites More sharing options...
.josh Posted July 25, 2009 Share Posted July 25, 2009 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? Quote Link to comment https://forums.phpfreaks.com/topic/167392-fgets-not-working/#findComment-882673 Share on other sites More sharing options...
abazoskib Posted July 25, 2009 Author Share Posted July 25, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/167392-fgets-not-working/#findComment-882678 Share on other sites More sharing options...
.josh Posted July 25, 2009 Share Posted July 25, 2009 possibly..is the file not a plain text file, stored as for instance encoded in utf-8, binary safe? Quote Link to comment https://forums.phpfreaks.com/topic/167392-fgets-not-working/#findComment-882685 Share on other sites More sharing options...
abazoskib Posted July 25, 2009 Author Share Posted July 25, 2009 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? Quote Link to comment https://forums.phpfreaks.com/topic/167392-fgets-not-working/#findComment-882709 Share on other sites More sharing options...
PFMaBiSmAd Posted July 25, 2009 Share Posted July 25, 2009 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? Quote Link to comment https://forums.phpfreaks.com/topic/167392-fgets-not-working/#findComment-882711 Share on other sites More sharing options...
ignace Posted July 25, 2009 Share Posted July 25, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/167392-fgets-not-working/#findComment-882862 Share on other sites More sharing options...
abazoskib Posted July 26, 2009 Author Share Posted July 26, 2009 opened it in a hex editor but saw nothing that marked the end of a line..care to explain what im looking for? Quote Link to comment https://forums.phpfreaks.com/topic/167392-fgets-not-working/#findComment-883487 Share on other sites More sharing options...
ignace Posted July 27, 2009 Share Posted July 27, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/167392-fgets-not-working/#findComment-883826 Share on other sites More sharing options...
abazoskib Posted July 27, 2009 Author Share Posted July 27, 2009 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? Quote Link to comment https://forums.phpfreaks.com/topic/167392-fgets-not-working/#findComment-883887 Share on other sites More sharing options...
ignace Posted July 27, 2009 Share Posted July 27, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/167392-fgets-not-working/#findComment-883908 Share on other sites More sharing options...
abazoskib Posted July 27, 2009 Author Share Posted July 27, 2009 that's really good to know. just did some research and it seems /r does not work with fgets. Quote Link to comment https://forums.phpfreaks.com/topic/167392-fgets-not-working/#findComment-883931 Share on other sites More sharing options...
abazoskib Posted July 27, 2009 Author Share Posted July 27, 2009 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); Quote Link to comment https://forums.phpfreaks.com/topic/167392-fgets-not-working/#findComment-884199 Share on other sites More sharing options...
.josh Posted July 27, 2009 Share Posted July 27, 2009 Try changing this: $search = "chr(13)"; $replace = "chr(10)"; $data = str_replace($search,$replace,$data); to this: $search = "\r"; $replace = "\n"; $data = str_replace($search,$replace,$data); Quote Link to comment https://forums.phpfreaks.com/topic/167392-fgets-not-working/#findComment-884231 Share on other sites More sharing options...
abazoskib Posted July 27, 2009 Author Share Posted July 27, 2009 did that too. didnt work. im about to just do it manually Quote Link to comment https://forums.phpfreaks.com/topic/167392-fgets-not-working/#findComment-884295 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.