11.07.2015 Views

PHP MySQL - Stilson.net

PHP MySQL - Stilson.net

PHP MySQL - Stilson.net

SHOW MORE
SHOW LESS
  • No tags were found...

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

CHAPTER 10 • WORKING WITH THE FILE AND OPERATING SYSTEMwww.it-ebooks.infoAle ale@example.comNicole nicole@example.comLaura laura@example.comThe following script reads in users.txt and parses and converts the data into a convenient Webbasedformat. Notice file() provides special behavior because unlike other read/write functions, youdon’t have to establish a file handle in order to read it:// Read the file into an array$users = file('users.txt');// Cycle through the arrayforeach ($users as $user) {}// Parse the line, retrieving the name and e-mail addresslist($name, $email) = explode(' ', $user);// Remove newline from $email$email = trim($email);// Output the formatted name and e-mail addressecho "$name ";This script produces the following HTML output:AleNicoleLauraLike fopen(), you can tell file() to search through the paths specified in the include_pathconfiguration parameter by setting use_include_path to 1. The context parameter refers to a streamcontext. You’ll learn more about this topic in Chapter 16.Reading File Contents into a String VariableThe file_get_contents() function reads the contents of a file into a string. Its prototype follows:string file_get_contents(string filename [, int use_include_path [, resource context [, intoffset [, int maxlen]]]])By revising the script from the preceding section to use this function instead of file(), you get thefollowing code:240

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!