Emad doom
Emad doom

Reputation: 87

What is the faster function to read files?

I am using fopen() and fread() to read files

if( file_exists( $file ) ){
    $open = fopen( $file , 'r' );
    return fread( $open , filesize( $file ) );
}
fclose( $file );

My files size is about 10 MB

So, I was wondering if there was anything faster.

file_get_contents seems to be faster, but in my searches I found it seems like it uses more ram memory... Which one should I use?

Upvotes: 3

Views: 472

Answers (2)

NullUserException
NullUserException

Reputation: 85478

I would recommend you to use file_get_contents() if all you want is to load the entire file into memory, since it's shorter and shows clearly what you are doing.

Also, from the PHP manual on file_get_contents():

file_get_contents() is the preferred way to read the contents of a file into a string. It will use memory mapping techniques if supported by your OS to enhance performance.

Upvotes: 2

genesis
genesis

Reputation: 50982

I'd use file_get_contents. I'd say user experience is main aspect you should think about

Upvotes: 1

Related Questions