PHP Programing language

File Input/Output in PHP
Previous Home Next
adplus-dvertising

File Input/Output

In php Files, you can learn how to create files, write files, read files and uploading files.

There are some file input/output methods:
  1. Open and Create a file
  2. Read a file
  3. Write a file
  4. Close a file

Modes of file:

There are some modes(access modifier) by using you can modified a file with file() funtions.

Mode Description
r
Opens the file for reading only.
Places the file pointer at the beginning of the file.
r+
Opens the file for reading and writing.
Places the file pointer at the beginning of the file.
w
Opens the file for writing only.
Places the file pointer at the beginning of the file. 
and truncates the file to zero length. 
If files does not exist then it attemts to create a file.
w+
Opens the file for reading and writing only.
Places the file pointer at the beginning of the file.
and truncates the file to zero length. If files does not
exist then it attemts to create a file.
a
Opens the file for writing only.
Places the file pointer at the end of the file.
If files does not exist then it attemts to create a file.
a+
Opens the file for reading and writing only.
Places the file pointer at the end of the file.
If files does not exist then it attemts to create a file.

Open and Create a file:

In Php, to open existing files and to create new files using fopen(). The fopen() function accepts two arguments. First argument is the name including the path of the file to open. And second argument is an attribute incicating the mode in which to open the file. It returns a file handle which is subsequently used for all future read and write interactions with that file.

Syntax:
fopen("filename", "access modifier");

Example:

<?php
fopen("fileOne.txt", "a");
?>
Read a file:

In Php, if you open a file by using fopen() function. It can be read by using fread() function. It can be file pointer and the length of the file expressed in bytes. The file length can be found by using filesize() function.

Syntax:
fgets($fileHandle)

Example:

<?php
$theFile = fopen("fileOne.txt", "r");
$theText = fgets($theFile);
print $theText;
?>
Write a file:

A new file can be written or text can be appended to an existing file by the use of  fwrite() function. This function requires two arguments one for file pointer and second for the string of data that is to be written. Optionally a third integer argument can be included to specify the length of the data to write. If the third argument is included, writing would will stop after the specified length has been reached.

Syntax:
fputs($fileHandle, $text);

Example:

<?php
$theFile = fopen("fileOne.txt", "w");
fputs($theFile, "line of text");
?>
Closing a File:

In Php, if any file open for reading or writing that file must be closed. This can only be done by using fclose() function.

Syntax:
fclose($fileHandle);

Example:

<?php
$theFile = fopen("fileOne.txt", "w");
fclose($theFile);
?>

File Functions in Php

Some file functions are as follows:

Function Description
basename() Returns the filename component of a path
chgrp() Changes the file group.
chmod() Changes the file mode.
chown() Changes the file owner.
copy() Copies a file.
delete() Deletes a file.
dirname() Returns the directory name component of a path.
fclose() Closes an open file.
fgetc() Returns a character from an open file.
fgets() Returns a line from an open file.
file() Reads a file into an array.
filesize() Returns the file size.
filetype() Returns the file type.
fopen() Opens a file or URL.
fputs() Alias of fwrite().
fread() Reads from an open file.
fscanf() Parses input from an open file according to a specified format.
fseek() Seeks in an open file.
fwrite() Writes to an open file.
popen() Opens a pipe.
pclose() Closes a pipe opened by popen().
Previous Home Next