Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • File input output

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 108
    Comment on it

    Opening files

    fopen() function is used to open the file. It require two arguement first the name and the other the mode.

    the different mode are

    r- open for read only

    r+- open file for read and writing

    w- open file for writing

    w+- open file for reading and writing only. If the file does not exist create the file.

    a- open file for writing only

    a+- open file for reading and writing.

    If it get fail to open the file it return false otherwise it return a file pointer.

     

    fclose() function is used to close the file.

     

    fread() function is used to read the function after the file is opened. It require two argument that are the first file pointer and the other is length of the file in byte.

    To read a file

    open using fopen() function.

    get length using filesize() function.

    read file using fread() function.

    close file using fclose() function.

    <html>
    
       <head>
          <title>Reading a file using PHP</title>
       </head>
       
       <body>
          
          <?php
             $filename = "tmp.txt";
             $file = fopen( $filename, "r" );
             
             if( $file == false ) {
                echo ( "Error in opening file" );
                exit();
             }
             
             $filesize = filesize( $filename );
             $filetext = fread( $file, $filesize );
             fclose( $file );
             
             echo ( "File size : $filesize bytes" );
             echo ( "<pre>$filetext</pre>" );
          ?>
          
       </body>
    </html>

    The result

    File size : 278 bytes

    The PHP Hypertext Preprocessor (PHP) is a programming 
    language that allows web developers to create dynamic 
    content that interacts with databases.
    PHP is basically used for developing web based software 
    applications. This tutorial helps you to build your base
     with PHP.

 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: