Title: Discussion PHP Files, ITSE 2402
Course: Intermediate Web Programming (25436)

IITSE 2402
PHP Files Chapter 5 Discussion
Discussion
1. Permissions
  - The ability to work with files depends on security permissions.
  - To set permissions on data files that you create after uploading the file, do the following
    - From Filezilla, right click on the file name.
    - Click on File Permissions.
    - Click on each check box in the Read and Write columns or simply enter 666 in the text box.
    - You should see 666 in the permission code.
    - Click OK. (If you use another file transfer program your goal is to end up with "chmod 666 for your data file.)
    - Every time you reupload the text file, you will need to go back and set the permission code. It does not always stay.
2. File Streams
  - A stream is used to access the data on a file
  - An input stream reads data from a file.
  - An output stream writes data to a file.
  - Like file streams, echo() and print() functions are output streams to the monitor.
  - You must open a file before you can read or write to it when using data incrementally.
  - After you have completed working with the file, you must close the file.
  - Exceptions in PHP are functions that will read the entire contents of a file. When using those functions, you are not required to use the fopen() and fclose() functions because they are built into the functions that read the entire contents.
  - To open or close a file, you use a special variable called a handle. A handle is used because you cannot use the name the operating system uses. The rules of identification are different for the operating system and for PHP identifiers.
3. Opening a File Stream
  - Use the fopen() function to open a handle to a file stream.
  - Suppose you wish to name your handle $open_file. The syntax for the fopen()using the $open_file handle is

                  $open_file = fopen("text file", "mode");

  - The mode argument can be

Mode Arguments
Argument | Description
a | Opens file for write only, pointing at the end of the file. If no file exists, a new file is created.
a+ | Opens file for reading and writing, pointing at the end of the file. If no file exists, a new file is created.
r | Opens file for read only, pointing at the beginning of the file.
r+ | Opens file for reading and writing, pointing at the beginning of the file.
w | Opens for write only, deletes any existing content in the file. If no file exists, a new file is created.
w+ | Opens the file for reading and writing, deletes any existing content in the file. If no file exists, a new file is created.
x | Opens the file for write only, returns false if the file already exists.
x+ | Opens the file for reading and writing, returns false if the file already exists.

4. Closing a File Stream
  - When you are finished working with a file stream you must close it.
  - The syntax is
fclose($handle);
5. Reading a line from a File
  - Whenever you issue the command to open a file, you must also test to make sure the file actually opened. (You have probably had an experience when you've chosen a drive and gotten a message that the drive is not available.)
  - When using $open_file = fopen("text file", "mode");
$open_file will be assigned either true are false depending on whether or not the file opened.
  - Usually we ask, if the file did not open, display a message, else process the file.

                  if (!$open_file)
                      {
                        echo "<p>Could not open the data file.</p>";
                        $balance = 0;
                      }
                      else
                      {// ...process the file...

  - To read a line from a file that has been opened use the fgets() function. You assign the return value to a variable.
  - You can designate a length, and the function will stop once that length is reached, or it will stop at end of file (EOF) if that comes first.
  - If the fgets() function fails, a false is returned.
  - Assign the value returned from the fgets() function to a variable.
6. Writing to a file
  - Use the fwrite() function to write data to an open file.
  - fwrite() returns the number of bytes written, or FALSE on failure.
  - There is an optional length argument, but we will not use it. The syntax is:

fwrite($fileHandle, "Hello World!");

  - You can display to the screen the number of bytes written or False:

echo fwrite($fileHandle, "Hello World!");

7. Writing Data to Files
  - Our server is a Linux server. To identify the end of a line in a file on a Linux server, you must append the escape sequence \n to signify the end of the line.
  - PHP has two basic functions to write to a text file. The file_put_contents() function and the fwrite() function--also called fputs() function.
    - file_put_contents() function
      - When writing the file_put_contents() function
        - if the file does not previously exist, a new file is created.
        - if the file exists, all data is overwritten unless you use a third argument FILE_APPEND
        - pass the name of the file as well as a text string with the data. You do this by assigning the file name variable with the external file name. For example:
$BowlersFile = "bowlers.txt";
        - you build a string variable with each line of data. If you build the string using several assignment statements, concatenate the variable after the first assignment using the .= operator.
      - The number of bytes that are written are returned. If the write fails, a 0 is returned.
    - fwrite() or fputs() functions
      - Used to write Data incrementally
      - Requires the use of the fopen() and fclose() functions.
        - When writing the fopen() function you specify
          - whether the file is a read or a write file
          - whether to create a file if one does not previously exist
          - whether to append or overwrite the file
8. Reading Data From Files
  - PHP has two function types to read from a text file. There are different functions that read the entire contents of a text and functions that read text incrementally.
    - file_get_contents() function
      - When reading the file_get_contents() function
        - If the data is a complete block (not written in individual records) you can read the entire file into a string variable and then display the entire string using an echo statement.
    - readfile() function
      - If you just want to read and print a file, you can use the readfile() function to go directly to a browser.
    - file() function
      - If you want to read a file that has been written with each line of text on a different line (a record) you can read the entire file into an array.
      - Once the data is in an array, use the explode() function to split the string where indicated by the call and assign to a new array.
  - Reading Data Incrementally
    - When reading data incrementally from a file, each time you read a line (record), the file pointer automatically advances to the next line. Each time you read a single character, the file pointer automatically advances to the next character.
    - When reading data, you should check to see if you have reached the end of the file so that you do not attempt to read past the end of file (which will cause your program to crash). The feof() function returns a true when you have reached the end of the file.
    - It is common to read data from a file in a loop that test for the end of the file. When the end of the file has not been reached, the next line or character is read.
    - Once you have opened your file and established that it did open, you are then ready to read from the file. When looping, you then ask, have I not read the end of file? You only want to continue if you have not read the end of file. For example:
while (!feof ($fp))