Fopen('r') vs File() PHP | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Fopen('r') vs File() PHP

Just finished the php course and have one question that was not discussed in the comments sections throughout. You can use the mode to open files for read only: fopen("filename.text", 'r'); But based on the PHP course, you use file("filename.text"), since that returns an array for you to use in an attempt to access the files. My question then is, what would you use fopen('r') for, if you can't write to it and is read from using file()? My assumption is that you can do $var = fread($file, 'file'); but I don't know if such a function even exists. I'll also google this 😅

25th Jul 2018, 4:35 PM
Andre Daniel
Andre Daniel - avatar
5 Answers
26th Jul 2018, 4:29 PM
Mickel
Mickel - avatar
+ 1
I'm not sure I understand some points of your thread, even so, this thread clarifies a part of it: https://stackoverflow.com/questions/24007898/difference-between-file-file-get-contents-and-fopen-in-php
26th Jul 2018, 3:23 AM
Mickel
Mickel - avatar
+ 1
Since I do not develop professionally with PHP I can not answer with certainty, but I usually use it to open all the files. For me it is much more comfortable to read them through the stream. File() is a high-level function, but I do not like to manipulate the contents of the file as an array. I find it impractical in many situations, especially because all operating systems use a different line break.
26th Jul 2018, 3:39 AM
Mickel
Mickel - avatar
0
Just confused on how to use fopen("file.txt", "r");
26th Jul 2018, 3:26 AM
Andre Daniel
Andre Daniel - avatar
0
I could not have any examples of using it with 'r' online. If you have time, can you give any?
26th Jul 2018, 11:19 AM
Andre Daniel
Andre Daniel - avatar