Main Content

File and URL reading in PHP

Archive - Originally posted on "The Horse's Mouth" - 2007-03-20 23:43:24 - Graham Ellis

In PHP, we say "there's a function to do that" as the answer to almost any question is going to be in the form of a function name, or a list of functions, to call. And PHP has more functions than almost any other language, making it more flexible than most.

Take file handling ... where in so many other languages you have just got open, read and close type constructs whereas in PHP you have those same constructs plus a whole lot of others

file lets you read the whole contents of a file into an array
fgetcsv lets you read a file line by line and break in into an array of tokens
file_get_contents lets you read the whole of a file into a string.

Impressive range already? Then remember that you can also give an URL as the first parameter when you open a file ... and that way the same set of functions allow you to read from another web page ....