My Blog

My WordPress Blog

My Blog

My WordPress Blog

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 […]

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 […]

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 […]

Scroll to top