user1159454
user1159454

Reputation: 3317

How important is fopen in PHP?

Hey so I'm trying to clean up my code a bit, and I just need to know: How important is the fopen function in PHP? By this I mean...well I've been told that you always need to fclose a file when you're done with it. This leads me to believe that if a file stays open too long then it gets corrupt in some way?

I don't know, but the thing is I've got an if statement that if the condition is true, it opens the file(s) and writes to it(them). Would it be just as efficient to open all the files for writing/reading at the beginning of the script, and then just include the instruction to actually write to them if the conditional is true??

And while we're on the topic...if I want to read line by line from a file I'll simply use the array = file("filename) shortcut I learned here. Is there a shortcut for writing to a file as well, without having to go through all the fopen stuff? Can I take a file and make it an array, line by line, and by changing that array, change the file? Is there anyway to do that?

Thanks!

Upvotes: 1

Views: 290

Answers (5)

deceze
deceze

Reputation: 522024

This leads me to believe that if a file stays open too long then it gets corrupt in some way?

No, it doesn't corrupt the file. It just uses up resources (opening or keeping a file handle open does take some time, memory and overhead) and you risk making other scripts that want to open the same file wait. The actual file handle will automatically be closed once your script ends, but it's a good idea to explicitly close it as soon as you're done with it. That goes for everything really: if you don't need it anymore, clean it up.

Would it be just as efficient to open all the files for writing/reading at the beginning of the script, and then just include the instruction to actually write to them if the conditional is true??

No, see above. Opening file handles isn't free, so don't do it unless you need to.

if I want to read line by line from a file I'll simply use the array = file("filename) shortcut I learned here

That's nice, but be aware that this reads the entire file into memory at once. For small files that hardly matters, but for larger files it means that a) your script will stop while the entire file is being read from the disk and that b) you need to have enough memory available to store the file in. PHP scripts are usually rather memory constrained, since there are typically many instances running in parallel.

Is there a shortcut for writing to a file as well, without having to go through all the fopen stuff?

file_put_contents

Upvotes: 0

Joni
Joni

Reputation: 111229

The number of files that a process can have open at a given time is limited by the operating system. If you open many files and never close them eventually you'll run out of your quota and you can't open any more.

On the other hand, if you open the file for writing, until you close the file you have no guarantee that what you have written is safely on the disk.

Upvotes: 1

Laradda
Laradda

Reputation: 101

Can I take a file and make it an array, line by line, and by changing that array, change the file?

You could make your own array class (implementing ArrayAccess interface), which loads every line of the file. Then modify those offsetSet and offsetUnset methods to rewrite the file everytime you call them.

But I doubt it will be performance wise to rewrite everything when you make a change.

Upvotes: 0

Mchl
Mchl

Reputation: 62369

The simple explanation is: until you fclose file, you have no guarantee that what you fwrited to it, is actually there. Operating system can have this content stored in some buffer and be waiting for access to hard disk. If you finish your script without closing the file, that data can simply be lost.

Now, this doesn't actually happen in majority of cases, but if youwant to be sure, fclose

Upvotes: 0

Rusty Fausak
Rusty Fausak

Reputation: 7525

if a file stays open too long then it gets corrupt in some way?

I think PHP is smart enough to garbage collect your open files when you are finishing using them. I don't think the file will be corrupted if you don't close it unless you write to it unintentionally.

Would it be just as efficient to open all the files for writing/reading at the beginning of the script

I'm not sure you should worry about efficiency unless you need to. If your code is working and is maintainable, I wouldn't change where you open files.

Is there a shortcut for writing to a file as well, without having to go through all the fopen stuff?

You can use file_put_contents(..) as a shortcut to write to files.

Upvotes: 2

Related Questions