• home
  • forum
  • my
  • kt
  • download
  • PHP Reading and Writing to a File Tutorial

    Author: 2008-08-26 08:45:30 From:

    In PHP before you can write to a file you have to open the file, then you can write to the file.

    To open a file in php is quiet simple

    <?php
    $myFile = "writeFile.txt";
    $fh = fopen($myFile, 'w');
    ?>
    

    'w' denotes that we want to open the file in write mode.

    The fwrite function in php allows data to be written to any type of file. fwrite requires two pieces of information (A) a file handle and (B) The information that is to be wrote to the file.

    The $fh variable contains the file handle for testFile.txt. The file handle knows the current file pointer, which for writing, starts out at the beginning of the file.

    After finishing writing Hello World to our file we have to close the file, passing it the file handle.

    fclose($fh);

    So now lets see an example:

    <?php
    $myFile = "writeFile.txt";
    $fh = fopen($myFile, 'w');
    
    $Data = "Hello World\n";
    fwrite($fh, $Data);
    fclose($fh);
    ?>
    

    discuss this topic to forum

    relation tutorial

    No relevant information

    Category

      Ad Management (4)
      Calendars (3)
      Chat Systems (7)
      Content Management (6)
      Cookies and Sessions (8)
      Counters (8)
      Database Related (8)
      Date and Time (9)
      Development (6)
      Discussion Boards (7)
      E Commerce (6)
      Email Systems (9)
      Error Handling (5)
      File Manipulation (22)
      Flash and PHP (6)
      Form Processing (19)
      Guestbooks (12)
      Image Manipulation (21)
      Installing PHP (7)
      Introduction to PHP (23)
      Link Indexing (8)
      Mailing List Management (9)
      Miscellaneous (53)
      Networking (8)
      News Publishing (9)
      OOP (24)
      PEAR (6)
      PHP vs Other Languages (2)
      Polls and Voting (6)
      Postcards (1)
      Randomizing (14)
      Redirection (11)
      Searching (9)
      Security (29)
      Site Navigation (16)
      User Authentication (14)
      WAP and WML (7)
      Web Fetching (8)
      Web Traffic Analysis (15)
      XML and PHP (16)

    New

    Hot