DamienRoche Posted September 22, 2008 Share Posted September 22, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/125234-solved-fread-file-or-get_file_contents/ Share on other sites More sharing options...
genericnumber1 Posted September 22, 2008 Share Posted September 22, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/125234-solved-fread-file-or-get_file_contents/#findComment-647384 Share on other sites More sharing options...
JasonLewis Posted September 22, 2008 Share Posted September 22, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/125234-solved-fread-file-or-get_file_contents/#findComment-647385 Share on other sites More sharing options...
DamienRoche Posted September 22, 2008 Author Share Posted September 22, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/125234-solved-fread-file-or-get_file_contents/#findComment-647392 Share on other sites More sharing options...
genericnumber1 Posted September 22, 2008 Share Posted September 22, 2008 Can you post the contents (or an example of the contents) of the file here in code tags? Quote Link to comment https://forums.phpfreaks.com/topic/125234-solved-fread-file-or-get_file_contents/#findComment-647393 Share on other sites More sharing options...
DamienRoche Posted September 22, 2008 Author Share Posted September 22, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/125234-solved-fread-file-or-get_file_contents/#findComment-647397 Share on other sites More sharing options...
genericnumber1 Posted September 22, 2008 Share Posted September 22, 2008 <?php $contents = file('test2.php'); $buffer = ''; foreach($contents as $line) { if($line != "\n") { $buffer .= "<p>$line</p>"; } } echo $buffer; ?> worked okay for me, but you said that wasn't working for you? Quote Link to comment https://forums.phpfreaks.com/topic/125234-solved-fread-file-or-get_file_contents/#findComment-647400 Share on other sites More sharing options...
.josh Posted September 22, 2008 Share Posted September 22, 2008 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(). Quote Link to comment https://forums.phpfreaks.com/topic/125234-solved-fread-file-or-get_file_contents/#findComment-647403 Share on other sites More sharing options...
DamienRoche Posted September 22, 2008 Author Share Posted September 22, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/125234-solved-fread-file-or-get_file_contents/#findComment-647405 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.