Register

If this is your first visit, please click the Sign Up now button to begin the process of creating your account so you can begin posting on our forums! The Sign Up process will only take up about a minute of two of your time.

Results 1 to 6 of 6
  1. #1
    Senior Member dotcommakers's Avatar
    Join Date
    Oct 2003
    Posts
    527
    Member #
    3342
    Liked
    1 times
    hello how can i add data to a text file by php... i know that it can be done but dont know how :classic:

  2.  

  3. #2
    Senior Member rosland's Avatar
    Join Date
    Jul 2003
    Location
    Norway
    Posts
    1,944
    Member #
    2096
    Don't know if this is what you mean:

    PHP Code:
    $filename="/path/to/file.txt";

    /* open a filepointer instructing what to do
    with the file (write/create/read/replace/append...) */
    $fp=fopen($fp"a");

    //write additional information to exsisting file, starting at the end
    fwrite($fp"content to be added");

    //close the filepointer
    fclose($fp); 
    file modes that can be specified to the filepointer:
    Code:
    r = Opens the file for reading only.
    Points to the beginning of the file
    
    r+ = Opens the file for reading and writing.
    Points to the beginning of the file
    
    w = Opens the file for writing only.
    Points to the beginning of the file and deletes existing content.
    If the file does not exsist, it attempts to create it.
    
    w+ = Opens the file for both reading and writing.
    Points to the beginning of the file and deletes existing content.
    If the file does not exsist, it attempts to create it.
     
    a = Opens the file for writing only.
    Points to the end of the file.
    If the file does not exsist, it tries to create it.
    
    a+ = Opens the file for both reading and writing.
    Points to the end of the file.
    If the file does not exsist, it tries to create it.
    S. Rosland

  4. #3
    Senior Member Fallout's Avatar
    Join Date
    Aug 2003
    Location
    Richmond, Virginia
    Posts
    543
    Member #
    2748
    Good explaination, that's exactly how you do it.

  5. #4
    Senior Member dotcommakers's Avatar
    Join Date
    Oct 2003
    Posts
    527
    Member #
    3342
    Liked
    1 times
    great help!!

    Thanks Rosland

    Great Help!!

    Just one request more can you tell me how can i read data from that txt file....

    thanks

  6. #5
    Senior Member rosland's Avatar
    Join Date
    Jul 2003
    Location
    Norway
    Posts
    1,944
    Member #
    2096
    If you just want to read from a file and echo the complete file, you can use the following code:

    PHP Code:
    //open a file pointer
    $fp=fopen("path/to/file.txt"'r');

    //check to see if there is a file there
    if(!fp)
     {
      echo 
    "file could not be found";
      exit;
     }

    //if the file exists
    while (!feof($fp))
     {
      
    $text=fgets($fp900);
      echo 
    $text.'<br/>;
     }

    //close file pointer
    fclose($fp); 
    The "feof" command, means File End Of File. The condition continous to be false until the filepointer reaches the end of the file (EOF).

    The function fgets, reads one line at the time from a file. In the example above, it will read either until it reaches a newline character (\n), encounters the EOF, or has read 889 bytes from the file. (length specified minus one byte.)For PHP versions after 4.2 the amount of bytes to be read defaults to 1024 if nothing is specified. For versions after 4.3, fgets() reads to the end of the file if nothing is specified.

    There are numerous ways of filereading options available, here are a few:
    fgets(int fp, int length) explained above

    fgetss(int fp, int length, string [allowable_tags]) same as above, but strips off any HTML or PHP tags found
    in the text. If you want to leave particular tags in, you can specify which in the allowable_tags parameter. This is a safety feature you might want to use when retracting text input from users (that might contain malicious code).

    array fgetcsv(intfp,intlength[, string delimiter[, enclosure]]) works the same way as fgets(), but breaks up the extracted string where it finds the delimiting character and stores it in an array. (example: fgetcsv($fp, 300, "\t"); would break up the extracted line wherever a TAB is encountered.

    The function readfile("path, to, file.txt"); reads the entire file in one go, and echoes the result to the browser before closing the file again.

    The file() function, reads the entire file and stores it in an array. Each line of the file is stored as a seperate element in the array. ($text_array=file($fp);

    From PHP v 4.3, you can use the function file_get_contents(). This works exactly like the readfile() function, but stores the information in a string rather than echoing it to the browser.

    The function fread() reads the specified amount of a file. The function takes two arguments, a filepointer and a length value. (fread($fp, 200); The function, in this case, reads up to 200 bytes of txt or to the end of the file (whichever comes first).

    The function filesize() returns the total amount of bytes in a file.
    PHP Code:
    //The following function reads the entire file and echoes it to the browser
    $fp fopen("path to file"'r');
    $sizefilesize("path to file");
    echo 
    fread($fp$size);
    fclose($fp); 

    unlink("path/to/file.txt") deletes the specified file.

    To search for a specific part of a string, there are a number of functions and techniques, depending on what you're looking for. If you are looking for an exact match, you can read my thread "Scraper" in this forum. If you want split a file at a certain point, or find a specific word, there are numerous ways of doing this.
    S. Rosland

  7. #6
    Senior Member dotcommakers's Avatar
    Join Date
    Oct 2003
    Posts
    527
    Member #
    3342
    Liked
    1 times
    thanks mate you done it

    thanks again


Remove Ads

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
All times are GMT -6. The time now is 08:02 AM.
Powered by vBulletin® Version 4.2.3
Copyright © 2019 vBulletin Solutions, Inc. All rights reserved.
vBulletin Skin By: PurevB.com