Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
  • Reading a file in PHP

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 48
    Comment on it

    PHP File handling concept allow us to do operation with any file in php.

    For reading a file we have to open it using fopen() function.Now for reading this file we have a function named as fread().

    fread function accept two arguments. First parameter is the file pointer and the other one is the length of the file in form of bytes.

    For getting length of file there is filesize() function which takes name of file as an argument and return us the size of file in bytes.

    So for reading a file below steps required in PHP.

    1. Open file by using fopen() function.
    2. Use filesize() function for Getting the file's length.
    3. Read the content of file now by using the fread() function.
    4. For closing file now use fclose() function.

    Example for reading a file :-

         $filename = "tmp.txt";
         $file = fopen( $filename, "r" );
         if( $file == false )
            echo ( "Error in opening file" );
         $filesize = filesize( $filename );
         $filetext = fread( $file, $filesize );
         fclose( $file );
         echo ( "File size : $filesize bytes" );
         echo ( "<pre>$filetext</pre>" );

 0 Comment(s)

Sign In

Sign up using

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: