meltingpoint Posted September 12, 2009 Share Posted September 12, 2009 Two questions. 1- At what size of a file does it become too cumbersome to uses file() as opposed to something like opening a file and then while !feof etc....? 5mb? 10mb? 2- Does file() open and close automatically? No need for fopen or fclose. Tks Quote Link to comment Share on other sites More sharing options...
Garethp Posted September 12, 2009 Share Posted September 12, 2009 For answer to number 2, just look at the example in php.net http://au2.php.net/manual/en/function.file.php No, it does not Quote Link to comment Share on other sites More sharing options...
Zyx Posted September 12, 2009 Share Posted September 12, 2009 Question one: of course it is much slower. PHP must split the file content into lines and pack it to arrays, and this takes some time. With feof() and fread() you simply read the file, you can process it on the fly or do something else with it. I remember a topic on one discussion group long ago - the guy complained about "very slow XML processing". He had a 10 MB file and tried to parse it with PHP, which took several minutes. He provided the source code and the next person noticed that he loads everything with file(), and then concatenates the whole array back to a string. After replacing this code with fopen() and fread(), the script became much faster and processed the same XML file in 2 seconds . You should avoid using file() unless you absolutely need the functionality it provides, especially for bigger files. Quote Link to comment Share on other sites More sharing options...
meltingpoint Posted September 12, 2009 Author Share Posted September 12, 2009 Thanks guys. That helps a lot. Cheers. Meltingpoint Quote Link to comment 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.