AV1611 Posted June 23, 2007 Share Posted June 23, 2007 I'm having a hard time understanding the difference between file() and file_get_contents() I usually use this when parsing a text file to a data base when the text file is on a website. sometimes one works, sometimes the other works when you do something like this: $lines=file('http://mysite.com/file.txt'); foreach($lines as $line_num => line){ ...etc... sometimes I have to do file_get_contents() instead... Can someone explain? Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/56878-file-vs-file_get_contents/ Share on other sites More sharing options...
Barand Posted June 23, 2007 Share Posted June 23, 2007 If you want to treat the contents of the file as an an array of lines of text, use file(); If you want to treat the contents of the file as one single string, use file_get_contents() Quote Link to comment https://forums.phpfreaks.com/topic/56878-file-vs-file_get_contents/#findComment-281014 Share on other sites More sharing options...
AV1611 Posted June 23, 2007 Author Share Posted June 23, 2007 Here is the problem I don't understand on my new webserver I built: I did this and it works: $lines=file_get_contents('http://site.com/file.txt'); $lines=nl2br($lines); $lines=explode("<br />",$lines); foreach($lines as $line_num => $line){ ... Isn't that the same as this? It produces no output...??? $lines=file('http://site/file.txt) foreach.... ?????????????????????????????? Quote Link to comment https://forums.phpfreaks.com/topic/56878-file-vs-file_get_contents/#findComment-281038 Share on other sites More sharing options...
AV1611 Posted June 23, 2007 Author Share Posted June 23, 2007 I figured it out... There was a ' in the text that was messing up my parsing of the array... I fixed it by escaping my quotes... I gotta watch that in the future... Quote Link to comment https://forums.phpfreaks.com/topic/56878-file-vs-file_get_contents/#findComment-281062 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.