lecture4 - CPSC431 Lecture4 File Related Functions Open,...

Info iconThis preview shows pages 1–6. Sign up to view the full content.

View Full Document Right Arrow Icon
CPSC431 Lecture4
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
File Related Functions Open, close and end-of-file <?php $fh = fopen("/home/www/data/users.txt", "rt"); while (!feof($fh)) echo fgets($fh); fclose($fh); ?> File open modes: r : Read-only. r+ : Read and write. w : Write only. w+ : Read and write. a : Write only. The file pointer is placed at the end of the file. a+ : Read and write. The file pointer is placed at the end of the file. b: Open the file in binary mode. t : Open the file in text mode.
Background image of page 2
File Related Functions Reading from a file Ale ale@example.com Nicole nicole@example.com Laura laura@example.com <?php $users = file("users.txt"); foreach ($users as $user) { list($name, $email) = explode(" ", $user); // Remove newline from $email $email = trim($email); echo "<a href=\"mailto:$email\"> $name</a> <br />\n"; } ?>
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
File Related Functions file_get_contents <?php $userfile= file_get_contents("users.txt"); // Place each line of $userfile into array $users = explode("\n",$userfile); foreach ($users as $user) { list($name, $email) = explode(" ", $user); echo "<a href=\"mailto:$email\"> $name/a> <br />"; } ?>
Background image of page 4
File Related Functions fgetc(): read one character fgetcsv(): read one word at a time fgets(): read one line fgetss(): similar to fgets(), strip tags fread(): read a number of bytes
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 6
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 12

lecture4 - CPSC431 Lecture4 File Related Functions Open,...

This preview shows document pages 1 - 6. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online