Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to open and read files with PHP

    • 0
    • 2
    • 1
    • 2
    • 0
    • 0
    • 0
    • 0
    • 1.04k
    Comment on it

    Before reading information from any file,we have to open the file for reading.

    This Blog will help you to find the code to read-open-close the file(s) on server we have created with PHP.

    1. Opening a file:fopen()

    This function enables to open a PHP written file in the specified mode.fopen() not only creates a connection to file but can also open URL

    $file_open = fopen("http://127.0.0.1/", "r");


    Function fopen( ) doesn't actually read the contents of a file.All it does is to set a pointer to the file which we want to open. It then returns what's call a file handle.

    Consider an example:

    $file_open=fopen("File_to_open.txt,'r');
    fclose($file_open);

    Here:
    1: Open the file. $file_open stores a reference to the file itself.

    2: 'r':defines mode(Here it's read mode).

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

    1. r - Open a file for read only. File pointer starts at the beginning of the file.

    2. w - Open a file for write only. Erases the contents of the file or creates a new file if it doesn't exist. File pointer starts at the beginning of the file.

    3. a - Open a file for write only. The existing data in file is preserved. File pointer starts at the end of the file. Creates a new file if the file doesn't exist.

    4. x - Creates a new file for write only. Returns FALSE and an error if file already exists

    5. r+ - Open a file for read/write. File pointer starts at the beginning of the file

    6. w+ - Open a file for read/write. Erases the contents of the file or creates a new file if it doesn't exist. File pointer starts at the beginning of the file

    7. a+ - Open a file for read/write. The existing data in file is preserved. File pointer starts at the end of the file. Creates a new file if the file doesn't exist

    8. x+ - Creates a new file for read/write. Returns FALSE and an error if file already exists

    The file has been open in the specified mode.Now, how do you read the contents of the file?

    PHP provides us with many file-reading functions

    1. fread()

    2. fgets()

    3. fgetc()

    4. readfile()

    5. file_get_contents()

    Lets talk about them individually :

    1. fread():
      This is one of the function for reading file contents.Its used to read from binary files specifying the number of bytes you wish to read in.

    Example:

    fread($file_to_be_read,filesize("file_to_be_read.txt"));

    1st parameter contains the name of the file, which is to read,and 2nd parameter specify the number of bytes to be read.In case,to read the entire file into a string filesize() function can be used.The function returns the length of a file, in bytes.

    1. fgets():
      This function is used to read file contents line-by-line.If your data has been separated with new lines then you could use fgets() function to read in one segment of data at a time. While using this function, you also need to check when the end of the file has been reached.This is done with the inbuilt function feof(i.e file, end of file).

    Example:

    $myfile = fopen("file_to_be_read.txt", "r") or die("Unable to open file!");
    // Output one line until end-of-file
    while(!feof($myfile)) {
      echo fgets($myfile) . "
    "; }
    1. fgetc()
      This is also one of many function for reading a file.Through this function you can read single character from a file.After a character is read,the file pointer moves to the next character in the file .

    Example:

    $myfile = fopen("file_to_be_read.txt", "r") or die("Unable to open file!");
    // Output one character until end-of-file
    while(!feof($myfile)) {
      echo fgetc($myfile);
    }
    1. readfile()
      The readfile( ) function read file contents and writes it to an output buffer.

    Example:

    $myfile = readfile("file_to_be_read.txt");

    This function on success returns the number of bytes read, or FALSE and an error on failure.

    1. file_get_contents()
      The file_get_contents() function is just one step up as compared with readfile().This function reads the contents of file into a string taking one parameter as input. This is the preferred way as it uses memory mapping techniques, if this is supported by the server,which is done to enhance performance.

    Example:

    $myfile = file_get_contents($file_to_be_read.txt);
      echo $myfile ;

    Or
    It can just be written in concise manner as:

    echo file_get_contents("myfile");

    Choose any of the above mentioned function and read your file(s).Enjoy!!

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: