7

I have a question regarding php and opening files

PHP has more than one function to open files:

file_get_contents()
fopen()
file()

My question is related to performance. On my server I have to open files, then check the contents, do some modifications if I need to, and resave them.

I want to make these changes as fast as possible

which one is better to open file (both small and large) performance wise? Meaning; why use one in preferance to the others?

1 Answer 1

8

file_get_contents() and file() both read the entire file into memory - difference being that one returns a string, while the other returns an array.

For small(ish) files, this may not be much of a performance hit for you.

For larger files, this can definitely make a tremendous impact. If the target file is of size 20MB, and you need to check a particular segment of data at some known distance into the file, fopen() and then seeking to that known position to get just the data you require would be much quicker, along the order of several magnitudes.

20MB is, of course, just an arbitrary number I pulled out of thin air, but consider that an arbitrarily large file just might exceed the resource usage limits imposed by your server upon your PHP environment.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.