Jump to content

[SOLVED] fread(), file(), or get_file_contents() ..?


Recommended Posts

So what do you think is best? say for opening a text file and putting the contents into a web page?

 

I've used fread() and file(), though I find file() easier...but I haven't really experimented with get_file_contents()..

 

Which would you advise to use? do any of them have any major drawbacks? less efficient? less flexible?

 

Damien.

 

 

Link to comment
https://forums.phpfreaks.com/topic/125234-solved-fread-file-or-get_file_contents/
Share on other sites

Use file_get_contents() or file() if you don't need the flexibility fopen()/fread()/fclose() will allow.

 

Deciding between file_get_contents() and file() depends upon what you need the data for.

 

I typically use fread() if I want to save memory by streaming the file by chunks or if I need the features it allows (such as ease of writing/reading for the same file).

I use file_get_contents() if I prefer simplicity and speed over memory efficiency.

I use file() if I need it line-by-line... such as in setting files or something similar.

If you want to just display the content then I would personally use file_get_contents().

 

It depends on the task at hand though. I don't use fread() anymore but depending on the job I'll either use file() or file_get_contents(). Both are good.

I see. So file_get_contents and file() seem to be very similar.

 

There is a purpose to this thread (now, any way). I've got the contents from a TXT file and displayed them on a page..trouble is, the sentences aren't style into paragraphs though they have line breaks in the text file. I can't figure out a reliable way to do.

 

For example, I tried with file() and then enclosed each line with <-p-> tags..though that split the text up, it obviously didn't work too well with lines being prematurely split by the tag.

 

I've just tried with file_get_contents() except I can't figure out how to style the text without tampering with the file...instead of altering the output.

 

Any advice? Does anyone know a decent way to tackle this? Thanks for making those points as well, very useful.

Yeh, sure.

 

Brennan the police dog received a commendation for bravery at a ceremony. He will be commended for his bravery and tenacity in tracing and apprehending two suspects who had fled from a stolen car after a collision. 

As a result of Brennan’s actions two men were arrested and convicted of taking a vehicle without consent.

 

Now the second paragraph does not split from the first. It sticks together, straight after 'collission.' This is directly from the .txt file, all line breaks intact. I should note, the text file scrolls horizontally for each paragraph so obviously the text is not wrapped, though there are line breaks.

 

Thanks.

from the manual:

 

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.

 

As others have pretty much said, unless you are wanting to use some function specific to file() (like the fact that it puts the lines into an array), use file_get_contents().

No, sorry. I meant I couldn't split the paragraphs in a reliable way. I've never even seen that code. Any way, that works great too, among the other 80 methods I have. Wow, php is..just...wow.

 

Thanks for all the input guys!- much appreciated.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.