Chapter 6: Storing Data with File I/O

Chapter 6: File I/O for Web Applications

Introduction to File I/O

  • File I/O (Input/Output) refers to the process of reading from and writing to files.

    • In a simple context, reading means viewing a file in a text editor (e.g., Google Docs), and writing means adding text using that editor.

    • As programmers, the goal is to read and write to files programmatically, allowing the file's information to be utilized as data within a program.

  • MVC Pattern Adherence:

    • Files containing data for a web application should ideally be placed in the model/database folder to follow the Model-View-Controller (MVC) pattern.

    • There is no strict technical restriction on file location; files can be placed anywhere accessible by the code with appropriate modification permissions.

    • The code responsible for manipulating files (opening, reading, writing, closing, etc.) must reside in the model folder.

  • File Permissions:

    • To allow reading and writing of data files, it may be necessary to change file permissions.

    • On Linux, this can be done using the chmod command (e.g., chmod 777 filename.txt). Permissions can also be manually changed via file explorers on various operating systems.

File Structure Symbols

Files contain data along with un-displayed symbols that determine text positioning and are crucial for programmatic navigation and data rendering across applications.

  • End of File (EOF):

    • Symbol: EOF (Symbolic) vs. Hello world! (Visual)

    • Description: Indicates that no more characters are available to read in the file.

    • Usage: Rarely checked manually by programmers; typically handled by back-end PHP read/write functions.

  • Newline Character (EOL):

    • Symbol: \n (On most OS) or \r\n (On Windows) (Symbolic) vs. Penny Cheng James Doe Elnaz Aghayeghi (Visual)

    • Description: Used to separate strings onto new lines.

      • \n is the line feed character (common on Linux/Mac).

      • \r\n combines carriage return and line feed characters (common on Windows).

    • Usage: Never manually checked for; generally handled by back-end PHP functions.

  • Delimiter:

    • Symbols: , (comma), / (forward slash), # (hash sign), ~ (tilde) (Symbolic) vs. Penny Cheng,100000,yearly (Visual)

    • Description: Used to separate strings on the same line to delineate distinct data fields.

    • Usage: Never manually checked for; handled automatically by specific PHP functions (e.g., fgetcsv()).

  • feof($file_handle) function:

    • Returns TRUE when the end of the specified file is reached.

    • Used to manually check for EOF, though usually handled automatically by PHP functions.

File Pointer

  • Concept: A file pointer is an internal marker that keeps track of the current position within a file, indicating the line or character being manipulated (read, written to, or appended).

  • Movement:

    • Reading/Overwriting: The file pointer starts at the beginning of the file (position 00) and moves forward based on the number of characters read or written.

    • Appending: The file pointer starts at the EOF character, immediately after the last character in the file.

  • fseek($file_handle, $new_position) function:

    • Description: Manually moves the file pointer to a new position, either forward or backward, searching for the desired position within the file.

    • Example: fseek($file_handle, 0); moves the pointer to the beginning of the file.

    • Recommendation: Generally, manual fseek() usage should be avoided as built-in read/write functions often manage the file pointer internally, but it can be useful for resetting the pointer to 00.

File Types

There are three common file types used for data storage:

  • TXT files:

    • Purpose: Basic text files without particular text organization.

    • Extension: .txt (e.g., mydocument.txt).

  • CSV files (Comma-Separated Values):

    • Purpose: Store tabular data where values are separated by commas. Readable by spreadsheet editors like Microsoft Excel.

    • Extension: .txt (e.g., mydata.txt).

    • Limitation: Cannot handle special characters (e.g., a comma as part of a value itself, which would be misinterpreted as a separator).

  • True CSV files:

    • Purpose: Similar to CSV but can handle special characters within values (e.g., by enclosing values in quotes).

    • Extension: .csv (e.g., myrealtdata.csv).

    • Difficulty: More complex to work with programmatically due to the need to handle escape characters and quoting rules.

File Permissions

Permissions control read, write, and execute access to files, managed by the operating system.

  • Linux chmod command:

    • Syntax: chmod 777 filename.txt

    • Purpose: Grants full read, write, and execute permissions to the owner, group, and others for filename.txt.

    • Usage: Navigate to the folder containing the data file and replace filename.txt with the actual file name.

  • Windows/Mac: Permissions can be changed via Powershell/command line or manually through File Explorer/Finder, typically by right-clicking the file and accessing properties/permissions.

PHP File Functions

Just like in a text editor, files must be opened before manipulation and closed afterward. Many file functions use a file handle, which is a variable containing a memory space with a connection to the file.

Open and Close Functions

These are essential first and last steps for file manipulation.

  • $file_handle = fopen($filename_path, $mode):

    • Description: Opens the specified file using the given mode and returns a file handle.

    • Example: $file_pointer = fopen("test.csv", "r");

  • fclose($file_handle):

    • Description: Closes the specified file, releasing the connection and any associated resources.

    • Example: fclose($file_pointer);

File Open Modes

Open modes dictate how a file can be accessed and manipulated. Using the most specific mode reduces potential errors.

  • r: Read

    • Opens the file for reading only. The file pointer starts at the beginning.

  • w: Write

    • Opens the file for writing only. The file pointer starts at the beginning of the file. If the file exists, its contents are truncated (overwritten) to zero length. If the file does not exist, it is created.

  • a: Append

    • Opens the file for writing only. The file pointer starts at the end of the file. If the file does not exist, it is created. New data is added to the end.

  • r+ or w+: Read and Write

    • Opens the file for both reading and writing. r+ starts at the beginning and preserves content unless overwritten. w+ truncates the file to zero length if it exists, otherwise creates it, and starts at the beginning.

  • a+: Read and Append

    • Opens the file for both reading and writing. The file pointer starts at the end of the file. If the file does not exist, it is created.

Read Functions

These functions automatically detect EOF and newline symbols, and CSV functions check for commas automatically.

  • file_get_contents($filename_path):

    • Description: Opens the file for reading, reads its entire contents, and returns it as a single string.

    • When to use: When the entire content is needed as one block. It's an all-in-one function (fopen(), fread(), fclose()).

    • Example 1: Demonstrates reading games.txt into a string, then explode()ing it by \n (or \r\n on Windows) to print each game on its own line.
      php $fileString = file_get_contents("games.txt"); $games = explode("\n", $fileString); foreach ($games as $game) { echo $game . "<br>"; }

  • file($filename_path):

    • Description: Opens the file for reading, reads all lines, and places each line into its own element of a returned array.

    • When to use: For easy looping over file contents line by line.

    • Example 2: Reads games.txt directly into an array of lines and prints each game.
      php $game_names_read = file("games.txt"); foreach($game_names_read as $game_name){ echo "<p> $game_name: </p>"; }

  • fread($file_handle, $len_bytes):

    • Description: Reads up to the specified number of bytes from the specified file.

    • When to use: When the number of bytes to read is known in advance. filesize() can be used to read all bytes.

    • Example 3: Reads all bytes from games.txt and echoes them.
      php $file_handle = fopen("games.txt","r"); echo fread($file_handle, filesize("games.txt")); fclose($file_handle);

  • readfile($filename_path):

    • Description: Does not return file contents but echoes them directly to the webpage.

    • When to use: Primarily for debugging purposes.

  • fgetcsv($file):

    • Description: Reads one line of a CSV file and returns an array of its comma-separated cells.

    • When to use: To read a True CSV file line by line, handling delimiters automatically.

Write Functions

Writing always overwrites data from the beginning of the file (if using modes like w or w+). New files are automatically created if they don't exist.

  • file_put_contents($filename_path, $data, $flag):

    • Description: Writes the specified data string to the named file. The $flag parameter can use FILE_APPEND to append instead of overwrite.

    • When to use: Recommended for writing TXT or CSV files as it's an all-in-one function (fopen(), fwrite(), fclose()) and includes file locking safeguards to prevent Race Conditions.

    • Example: Appends num_players to game.txt.
      php $filename_path = "game.txt"; $num_players = 5; file_put_contents($filename_path, $num_players);

  • fwrite($file_handle, $data):

    • Description: Writes the specified string data to the specified file. Alias: fputs().

    • When to use: The basic write function for TXT or CSV files, used when you're managing the file handle yourself.

  • fputcsv($file, $array):

    • Description: Writes the specified array to the specified file as a line of comma-separated values.

    • When to use: When working with True CSV files, it correctly formats and writes arrays as CSV rows.

  • Mini Exercise Solution: Demonstrates reading words.txt, processing each word to count characters, and writing the structured output to words_counted.txt using file(), fopen('w'), trim(), strlen(), and fwrite().

Managing Files (Uploads)

To allow users to upload files to a server, several components are involved.

  • Required Components:

    1. Changing directives in the php.ini Apache Server configuration file.

    2. Using the HTML input type="file" tag.

    3. Leveraging the $_FILES superglobal variable.

    4. Employing PHP functions for moving files.

Step 1: Configure php.ini

The Apache Server configuration file, php.ini, needs appropriate settings for file uploads. Comments in .ini files start with a semicolon (;).

  • file_uploads=On: Ensure this directive is set to On to allow HTTP file uploads.

  • Restrictions on Uploaded Files:

    • ;upload_tmp_dir =: Specifies the temporary directory for uploaded files (defaults to system default if not set).

    • upload_max_filesize = 16M: Sets the maximum allowed size for uploaded files (example: 1616 Megabytes).

    • max_file_uploads = 20: Sets the maximum number of files that can be uploaded via a single request (example: 2020 files).

    • post_max_size = 20M: Sets the maximum size of POST data PHP will accept, affecting total upload size (example: 2020 Megabytes).

    • max_input_time = 60: Sets the maximum time (in seconds) PHP will spend parsing request data.

    • memory_limit = 128M: Sets the maximum amount of memory (in bytes) a script is allowed to allocate.

    • max_execution_time = 30: Sets the maximum time (in seconds) a script is allowed to run.

    • Note: For basic use, these values may not need changing, but they can be adjusted for stricter or more lenient policies.

Step 2: HTML Form for Upload

The .phtml file must include specific HTML attributes to enable file uploads.

  • Input Element: type="file" is mandatory (e.g., <input type="file" id="file_id_prop" name="file_name_prop">).

  • Form Element: method="POST" is mandatory.

  • Form Element: enctype="multipart/form-data" specifies that the form data includes uploaded files.
    html ¨K50K ¨K51K

  • Alternative: An empty file can also be created programmatically using the write mode (w), which automatically creates a new file if one doesn't exist.

Step 3: $_FILES Superglobal Variable

The $_FILES superglobal variable is accessible anywhere in a PHP application and contains information about uploaded files.

  • $_FILES['file_name_prop']['tmp_name']:

    • Variable contains: The path where the file is temporarily uploaded on the server. This path is automatically chosen by the server.

  • $_FILES['file_name_prop']['name']:

    • Variable contains: The original name of the file as submitted by the user.

  • $_FILES['file_name_prop']['size']:

    • Variable contains: The size of the uploaded file in bytes.

  • $_FILES['file_name_prop']['type']:

    • Variable contains: The MIME type of the file (e.g., image/jpeg, text/plain).

  • DIRECTORY_SEPARATOR Constant:

    • A predefined PHP constant (\ for Windows, / for Mac/Linux) used to make file path strings platform-independent.

Step 4: Moving Uploaded Files

After a file is temporarily uploaded, PHP functions are used to move it to a permanent, desired location, often within the model folder.

  • getcwd():

    • Description: Gets the current working directory of the script.

  • mkdir($dir_name, $permissions, $recursive):

    • Description: Creates a new directory with the specified name.

    • Example: mkdir($new_dir_path, 0777, true); (creates actual_curr_dir/model/images with full permissions and recursively if parent directories don't exist).

  • move_uploaded_file($from, $to):

    • Description: Moves an uploaded file from its temporary location ($from) to a new destination ($to). Crucial for securely handling uploads.
      php $new_dir_path = getcwd() . DIRECTORY_SEPARATOR . 'model' . DIRECTORY_SEPARATOR . 'images' . DIRECTORY_SEPARATOR; mkdir($new_dir_path, 0777, true); $new_file_path = $new_dir_path . $_FILES['file_name_prop']['name']; $file_loc = $_FILES['file_name_prop']['tmp_name']; $success = move_uploaded_file($file_loc, $new_file_path); if ($success) { $upload_message = $new_file_path . ' has been uploaded and moved.'; }

More File Management Functions
  • is_file($path):

    • Description: Checks if the given path leads to a file (returns TRUE for files, FALSE for directories).

  • is_dir($path):

    • Description: Checks if the given path leads to a directory (returns TRUE for directories, FALSE for files).

  • file_exists($path):

    • Description: Checks if a file or directory defined by the given path exists.

  • scandir($path):

    • Description: Returns an array of the files and folders within the specified directory.

    • Example: scandir(getcwd());

  • copy($oldname, $newname):

    • Description: Copies a file from $oldname to $newname.

    • Example: copy("profile_pic", "bkup_profile_pic");

  • rename($oldname, $newname):

    • Description: Renames a file or directory from $oldname to $newname.

    • Example: rename("profile_pic", "prfl_pic");

  • unlink($name):

    • Description: Deletes a file from the server.

    • Example: unlink("old_file.txt");

Output Buffer

  • String Buffer: An intermediary memory space where text is temporarily held before being written to a file or displayed on a device (e.g., monitor, braille display, phone screen).

  • Purpose: Acts as a