Unit 10: Working with Files

4th semester

What is a File?

A file is simply a resource for storing information on a computer.
Files are usually used to store information such as;

  • Configuration settings of a program
  • Simple data such as contact names against the phone numbers.
  • Images, Pictures, Photos, etc.

Opening a File
The fopen() function is used to open files in PHP.The first parameter of this function contains the name of the file to be opened and the second parameter specifies in which mode the file should be opened:

Different Types Of File MODE

The file may be opened in one of the following modes:

Modes Description
w Write only. Opens and clears the contents of file; or creates a new file if it doesn’t exist
w+ Read/Write. Opens and clears the contents of file; or creates a new file if it doesn’t exist
r Read only. Starts at the beginning of the file
r+ Read/Write. Starts at the beginning of the file
a Append. Opens and writes to the end of the file or creates a new file if it doesn’t exist
a+ Read/Append. Preserves file content by writing to the end of the file
x Write only. Creates a new file. Returns FALSE and an error if file already exists
x+ Read/Write. Creates a new file. Returns FALSE and an error if file already exists


Note:
If the fopen() function is unable to open the specified file, it returns 0 (false).
Example
The following example generates a message if the fopen() function is unable to open the specified file:

Closing a File

 The fclose() function is used to close an open file:

PHP Read File

PHP provides various functions to read data from file. There are different functions that allow you to read all file data, read data line by line and read data character by character.
The available PHP file read functions are given below.

  • fread()
  • fgets()
  • fgetc()

Read File – fread()

The PHP fread() function is used to read data of the file. It requires two arguments: file resource and file size.

Syntax


$handle represents file pointer that is created by fopen() function.

$length represents length of byte to be read.

Reading a File Line by Line

 The fgets() function is used to read a single line from a file.
Note: After a call to this function the file pointer has moved to the next line.
Example
The example below reads a file line by line, until the end of file is reached

Reading a File Character by Character

The fgetc() function is used to read a single character from a file.
Note: After a call to this function the file pointer moves to the next character.
Example

PHP Write File

PHP fwrite() and fputs() functions are used to write data into file. To write data into file, you need to use w, r+, w+, x, x+, c or c+ mode.

PHP Write File – fwrite()

The PHP fwrite() function is used to write content of the string into file.

Output: data.txt

PHP file upload

allow users to upload files from a form can be very useful. Look at the following HTML form for uploading files:

Description of above form contents

· enctype: is the attribute that will define that the file to be uploaded is being uploaded using HTML form.
· action: This attribute is used to define the reference of PHP file, that is responsible to handle code for uploading PHP file.
· method:This attribute is used to define the way to transferring mode of contents to the PHP file. It can be GET and POST.
· input type: Using this attribute we can define the maximum size of file in bytes, that can be uploaded.
· input name: This attribute is used to define the name of uploaded file.