Author: Awais Farooq
-
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.…
-
$_COOKIE
The PHP superglobal $_COOKIE stores the variables passed to the current PHP script along with the HTTP request in the form of cookies. $HTTP_COOKIE_VARS also contains the same information, but it is not a superglobal, and it has now been deprecated. What is a Cookie? Cookies are text files stored by a server on the…
-
$_ENV
$_ENV is a superglobal variable in PHP. It is an associative array that stores all the environment variables available in the current script. $HTTP_ENV_VARS also contains the same information, but it is not a superglobal, and it has now been deprecated. The environment variables are imported into the global namespace. Most of these variables are provided by the…
-
$_FILES
$_FILES is one of the ‘superglobal’, or automatic global, variables in PHP. It is available in all scopes throughout a script. The variable $_FILES is an associative array containing items uploaded via HTTP POST method. A file is uploaded when a HTML form contains an input element with a file type, its enctype attribute set…
-
$_GET
$_GET is one of the superglobals in PHP. It is an associative array of variables passed to the current script via the query string appended to the URL of HTTP request. Note that the array is populated by all requests with a query string in addition to GET requests. $HTTP_GET_VARS contains the same initial information,…