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 Link to comment https://forums.phpfreaks.com/topic/174023-file/ 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 Link to comment https://forums.phpfreaks.com/topic/174023-file/#findComment-917327 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. Link to comment https://forums.phpfreaks.com/topic/174023-file/#findComment-917333 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 Link to comment https://forums.phpfreaks.com/topic/174023-file/#findComment-917341 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.