Category: 07. File Handling

  • Listing Files

    Windows command DIR and Linux command ls both display the list of files in the current directory. These commands can be operated with different switches to apply conditions on the list of files displayed. PHP provides a couple of options for programmatically listing files in a given directory. The readdir() Function The opendir() function in…

  • Create Directory

    Computer files are stored in the local storage device (called drive) in a hierarchical order, where a directory contains one or more files as well as subdirectories. Respective DOS commands defined in operating systems Windows, Linux etc. are used to create and manage directories. PHP provides directory management functions to create a directory, change the current…

  • File Permissions

    The concept of permissions is at the core of Unix/Linux file system. The permissions determine who can access a file and how one can access a file. File permissions in Linux are manipulated by the chmod command, which can be run inside the Linux terminal. PHP provides the chmod() function with which you can handle file permissions programmatically.…

  • Handle CSV File

    Popular spreadsheet programs use the CSV file format (which stands for Comma Separated Values) to export worksheet data in plain text. Each line in the file represents one row of the worksheet, with values in each column separated by commas. PHP’s filesystem function library provides two functions – fgetcsv() and fputcsv() – respectively to read data from a CSV…

  • Delete File

    PHP doesn’t have either a delete keyword or a delete() function. Instead, it provides the unlink() function, which when called, deletes a file from the filesystem. It is similar to Unix/C unlink function. If the delete operation could not be completed, PHP returns false and shows an E_WARNING message. The mandatory string parameter to unlink() function is a string that refers…

  • Append File

    In PHP, the fopen() function returns the file pointer of a file used in different opening modes such as “w” for write mode, “r” read mode and “r+” or “r+” mode for simultaneous read/write operation, and “a” mode that stands for append mode. When a file is opened with “w” mode parameter, it always opens…

  • Copy File

    You can copy an existing file to a new file in three different ways − Method 1 In the first approach, you can read each line from an existing file and write into a new file till the existing file reaches the end of file. In the following PHP script, an already existing file (hello.txt)…

  • Download File

    Most modern browsers allow files of certain types to be downloaded automatically, without any server-side code such as a PHP script. For example, a zip file, or an EXE file. If an HTML hyperlink points to a ZIP or EXE file, the browser downloads it and pops up a save dialog. However, text files, image…

  • File Existence

    It is often handy to check if the file you are trying to open really exists in the first place before performing any processing on it. Otherwise, the program is likely to raise a runtime exception. PHP’s built-in library provides some utility functions in this regard. Some of the functions we shall discuss in this chapter…

  • Write File

    PHP’s built-in function library provides two functions to perform write operations on a file stream. These functions are fwrite() and fputs(). To be able to write data in a file, it must be opened in write mode (w), append mode (a), read/write mode (r+ or w+) or binary write/append mode (rb+, wb+ or wa). The fputs() Function The…