PHP File handling and types

aimtocode

Definition Of File?

A file is simply a resource for storing information on a computer. PHP File System allows us to create file, read file line by line, read file character by character, write file, append file, delete file and close file. PHP provides a convenient way of working with files via its rich collection of built in functions.

PHP File Formats

  • File.txt
  • File.log
  • File.custom_extension eg. file.xyz
  • File.csv
  • File.gif, file.jpg etc
  • Files provide a permanent cost effective data storage solution for simple data compared to databases that require other software and skills to manage DBMS systems. File Funciton

File Opening-fopen():

The PHP fopen() function is used to open a file.

Syntax:

Here,

  • "fopen": is the PHP open file function
  • "$file_name": is the name of the file to be opened
  • "$mode": is the mode in which the file should be opened.
ModeDescription

r

Read only mode, file pointer will set at the starting position of file.

r+

Read and write mode, file pointer will set at the starting of file.

w

Write mode only, if the file exists it will open otherwise a new file will be created. If the file already exists, earlier contents will be overwritten

w+

Write and read mode, if file exists contents will be overwritten otherwise a new file will be created.

a

Append file, if file exists it will open and file pointer will set at the ending of file, if it doesn’t exists a new file will be created.

a+

Read and append mode, if file exists the pointer will set at the end of file, otherwise a new file will be created.

x

It is used to create and open a file only for writing purpose, if file exists this mode will return false and display an error. If file doesn’t exists it will simply create a file


Example:



PHP File Reading- fread():

Once a file is opened using fopen() function then it can be read by a function called fread()

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

The files size can be calculated using the filesize() function which takes the file name as its argument and returns the size of the file in bytes.

Syntax:


   string fread ( resource $handle , int $length )  


Example:




Reading a File Line by Line




PHP File Writting-fwrite():

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

Syntax:

    int fwrite ( resource $handle , string $string [, int $length ] )  

Example:

Here,

  • “fwrite” is the PHP function for writing to files.
  • “$handle” is the file pointer resource.
  • “$string” is the data to be written in the file.
  • “$length” is optional, can be used to specify the maximum file length.


PHP File Closing-fclose():

The PHP fclose() function is used to close an open file pointer.

Syntax:

  ool fclose ( resource $handle ) 

Example:


Here,

  • “fclose” is the PHP function for closing an open file
  • “$handle” is the file pointer resource.


PHP Copy Function-copy():

The PHP copy function is used to copy files.

Syntax:

  copy($file,$copied_file);

Example:



Here,

  • “$file” specifies the file path and name of the file to be copied.
  • “copied_file” specified the path and name of the copied file.


PHP File Deleting-unlink():

The unlink function is used to delete the file

  bool unlink ( string $filename [, resource $context ] ) 

Example:



Read Also:

aimtocode