File System in PHP

File System in PHP

File system in PHP, as the words suggests, it is the system to manage the files of the server. To understand the details of the process of file system in PHP completely, we first need to clear our concepts as to what exactly is a file. A file may be a text document (notepad, word, etc), an image, a spreadsheet or anything similar. Proper management and functioning of the files is very essential for a good application to work smoothly.

Managing a file in a file system could include creation of a file, reading a file, writing content to a file, editing a file or closing a file. All the above have their own significance. To perform these operations, PHP has special functions dedicated for the purpose. The collection of these functions and their behaviour, is termed as the file system in PHP. To implement these functions, we do not need any installations or any specific type of interface. We will now discuss the functions used for file system in PHP. Their behaviour is influenced by the properties we set in the file ‘php.ini’. This ‘php.ini’ file could be found in the server folder saved on your desktop.

File system in PHP is compatible with all types of systems. There is just a slight difference between the path definition in unix and windows. In unix system, the directory names are separated using the forward slash(/) whereas in windows, both, forward slash(/) and backslash(\) can be used.

Dividing the process into three steps, we discuss them in the following section.

filesystem in php tutorial

Opening a file:

To open a file, we use the fopen() function. The syntax for the function is like:
             fopen(filename, mode, include_path, context);                       

In the above syntax,

  • the filename is the name of the file to be opened.
  • the mode is the access type of the file. It is defined using single character.
    • r – read only type
    • r+ – read and write type
    • w – write only type. Clears the file and starts writing.
    • w+ – read and write type
    • a – write only type. Writes at the end of the file preserving previous data.
    • a+ – read and write type. Writes at the end of the file preserving previous data.
    • x – write only type. Creates a new file to write the data.
    • x+ – read and write type. Creates a new file to write the data.
    • the include_path is set to 1 if we want to search for the file in php.ini also.
  • the context is a set of options that can modify the behaviour of stream.

Working on the file:

To perform different actions on the file using file system in PHP, we have various functions. We will discuss about some of them here.

  • chmode() – It is a function used to change the mode of the file. Its syntax goes like:chmode(file,mode);
    where file is the name of the file and mode is the new set of permissions for the file. It consist of 4 numbers, first one is always zero. The second one defines the permission for the owner, third one for his group and the fourth one for everybody else.
  • copy() – It is a function to copy one file to the other. Its syntax goes like:copy(file, to_file);
    where file is the name of the file to be copied and to_file is the name of the file to which it has to be copied.
  • dirname() – It is a function which tells us the directory name from the given path. Its syntax goes like:dirname(path);
    where path is the path from which the directory name is to be found.
  • disk_total_space() – It is a function which tells us the total space in the directory in bytes. Its syntax goes like:disk_total_space(directory);
    where directory specifies the directory name which is to be checked.
  • feof() – It is a function used to find if we have reached the end of the file. Its syntax goes like:feof(file);
    where file is the name of the file to be checked. The function returns true if the end has been reached, else fale.
  • file_exists() – It is the function used to check whether the file exists in the given location or not. Its syntax goes like:file_exists(path);
    where path is the location of the file to be checked. It returns true if the file is present and false otherwise.
  • fileperms() – It is a function which gives us the information about the permissions set on a file. Its syntax goes like:fileperms(filename);
    where filename is the name of the file to be checked. It returns the 4 digit permission numeral.
  • filesize() – It is a function which returns the size of a file. Its syntax goes like:filesize(file);
    where file is the name of the file for which the size is to be known. It returns the size of the file in bytes if the file is found, else returns false.
  • filetype() – It is a function which tells us the type of the specified file. Its syntax goes like:filetype(filename);
    where filename is the name of the file to be checked. The function returns one of the following value for the file type:
  • fifo
  • char
  • dir
  • block
  • line
  • file
  • unknown
     
  • fread() – It is a function which reads data from an open file. Its syntax goes like:
  • fread(filename, length);
    where filename is the name of the file from which the data is to be read and length is the maximum number of bytes which are to be read. If not specified, the file is read till the end.
  • fwrite() – It is a function which writes data to the file. Its syntax goes like:fwrite(file, string, length);
    where file is the name of the file to which the data is to be written, string is the set of characters which are to be written and length is the maximum number of bytes which can be written.
  • is_readable() – It is a function which checks if the given file is in the readable mode or not. Its syntax goes like:is_readable(filename);
    where filename is the name of the file to be checked.
  • rename() – It is a function used to rename a file. Its syntax goes like:rename(oldname, newname, context);
    where oldname is the name of the file to be renamed, newname is the new name to be given to the file and context specifies the context of file handling.
  • rewind() – It is a function which moves the position of the file pointer to the beginning of the file. Its syntax goes like:rewind(filename);
    where filename is the name of the file in which the pointer is to be moved.Closing a file:To close a file of file system in PHP, we use the fclose() function. The syntax for the function is like:     fclose(filename);where the filename is the name of the file to be closed.

Leave a Reply

You must be logged in to post a comment.

Copy link