WTA10 PHP File Cookies Session

WTA10 PHP File Cookies Session - PHP File Handling Sessions...

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

View Full Document Right Arrow Icon
1 PHP File Handling Sessions Cookies 1 Reading a File line by line <?php $file = fopen("textfile.txt", "r"); while(!feof($file)) { echo fgets($file). "<br />"; } fclose($file); ?> 2 Before you read a file you need to open it. We're using PHP's fopen() function and providing it with two parameters: the file name and the mode in which it should be opened. We then loop through the file using PHP's feof() function. This checks for the end of the file. Our loop specifies that, while the end of file has not been reached, process the code inside the loop. The code inside the loop uses PHP's fgets() function. This function reads the file line by line. We need to put our own break otherwise each line would end up on the same line. Finally, we close the file using PHP's fclose() function, passing the opened file as a parameter.
Background image of page 1

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

View Full DocumentRight Arrow Icon
2 Reading a File character by character <?php $file=fopen(“textfile.txt","r") or exit("Unable to open file!"); while (!feof($file)) { echo fgetc($file); } fclose($file); ?> 3 The fgetc() function is used to read a single character from a file. After a call to this function the file pointer moves to the next character. File Permissions 4
Background image of page 2
3 Writing to a File <?php $file = fopen("textfile.txt", “w”); fwrite($file, "File contents go here. .."); fclose($file); ?> 5 We use PHP's fopen() function and supply it with two parameters: the file name and the mode in which it should be opened. We then use PHP's fwrite() function to write to the file. We supply two parameters: the opened file, and the text to go inside the file. Finally, we close the file using PHP's fclose() function, passing the opened file as a parameter. Appending to a File <?php $file = fopen(“textfile.txt", “a”); fwrite($file, "Appended file contents go here. .."); fclose($file); ?> 6 The previous code overwrites any content that might have been in the file. If you only want to add to the end of the existing data, you can simply change the mode from "w" (for "write") to "a" (for "append").
Background image of page 3

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

View Full DocumentRight Arrow Icon
Deleting a File <?php unlink(“textfile.txt"); ?> 7 To delete a file, you use PHP's unlink() function and provide it with the name/path of the file to delete. Session
Background image of page 4
Image of page 5
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 15

WTA10 PHP File Cookies Session - PHP File Handling Sessions...

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

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