Category: 12. PHP
-
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…
-
Read File
There are a number of options in PHP for reading data from a file that has been opened with the fopen() function. The following built-in functions in PHP’s library can help us perform the read operation − The fgets() Function The fgets() function can return a line from an open file. This function stops returning on a…
-
Open File
PHP’s built-in function library provides fopen() function to open a file or any other stream and returns its “reference pointer”, also called as “handle”. The fopen() function in PHP is similar to fopen() in C, except that in C, it cannot open a URL. Syntax of fopen() The fopen() function has the following signature −…
-
$_SESSION
One of the superglobal variables in PHP, $_SESSION is an associative array of session variables available in the current script. $HTTP_SESSION_VARS also contains the same information, but it is not a superglobal, and it has now been deprecated. What is a Session? A Session is an alternative way to make data accessible across the pages of an entire website.…