37 users online (0 members and 37 guests)  


  Results 1 to 5 of 5

Related

  1. Checkboxes and arrays    Forum: Javascript Forum
    Replies: 18
  2. Problem with arrays and forms    Forum: Javascript Forum
    Replies: 21
  3. Replies: 0
  4. Help, i need help with .htc files..    Forum: CSS Forum
    Replies: 3
  1. #1
    r4874C's Avatar
    New User

    Status
    Offline
    Join Date
    Mar 2006
    Posts
    7

    operations with files and arrays

    operations with files and arrays

    1. this function adds 1 line to the bottom of the file
    is it possible to add lines to the top?



    $gbfile='gb.txt';

    function add($str){
    global $gbfile;
    $tmp = trim($str);
    $fp=fopen($gbfile,'a+');
    flock($fp, LOCK_EX);
    fwrite($fp, $tmp. "\n");
    flock($fp, LOCK_UN);
    fclose($fp);
    }


    2. there is an array consisting of text lines $lines = file ('entries.txt');
    how to reverse an array (make first line last, last first and so on)

    3.
    this function adds 1 line to the bottom of the file
    how to rewrite a file (remove contents of a file and write information into a file)?



    $gbfile='gb.txt';

    function add($str){
    global $gbfile;
    $tmp = trim($str);
    $fp=fopen($gbfile,'a+');
    flock($fp, LOCK_EX);
    fwrite($fp, $tmp. "\n");
    flock($fp, LOCK_UN);
    fclose($fp);
    }



  2. #2
    DeadMeatGF's Avatar
    Moderator

    Status
    Offline
    Join Date
    Sep 2005
    Posts
    381

    Re: operations with files and arrays

    Quote Originally Posted by r4874C
    3. this function adds 1 line to the bottom of the file
    how to rewrite a file (remove contents of a file and write information into a file)?
    PHP Code:
    $gbfile='gb.txt';function add($str){
    global 
    $gbfile;
          
    $tmp trim($str);
          
    $fp=fopen($gbfile,'a+'); 
               
    flock($fpLOCK_EX); 
                    
    fwrite($fp$tmp"\n"); 
               
    flock($fpLOCK_UN); 
          
    fclose($fp); 

    If you use
    PHP Code:
    $fp=fopen($gbfile,'w+'); 
    it will clear all existing data from the file.
    Quote Originally Posted by www.php.net
    'w+' Open for reading and writing; place the file pointer at the beginning of the file and truncate the file to zero length. If the file does not exist, attempt to create it.

  3. #3
    DeadMeatGF's Avatar
    Moderator

    Status
    Offline
    Join Date
    Sep 2005
    Posts
    381

    Re: operations with files and arrays

    Quote Originally Posted by r4874C
    1. this function adds 1 line to the bottom of the file
    is it possible to add lines to the top
    $gbfile='gb.txt'
    function add($str){
    global $gbfile;
    $tmp = trim($str);
    $fp=fopen($gbfile,'a+');
    flock($fp, LOCK_EX);
    fwrite($fp, $tmp. "\n");
    flock($fp, LOCK_UN);
    fclose($fp);
    }[/PHP]
    Quote Originally Posted by kzevian via php.net
    I needed to append, but I needed to write on the file's beginning, and after some hours of effort this worked for me:

    PHP Code:
    $file "file.txt";
    if (!
    file_exists("file.txt")) touch("file.txt");
    $fh fopen("file.txt""r");
    $fcontent fread($fhfilesize("file.txt"));

    $towrite "$newcontent $fcontent";

    $fh2 fopen('file.txt''w+');
    fwrite($fh2$towrite);
    fclose($fh);
    fclose($fh2); 
    Hope that helps ...

  4. #4
    DeadMeatGF's Avatar
    Moderator

    Status
    Offline
    Join Date
    Sep 2005
    Posts
    381

    Re: operations with files and arrays

    Quote Originally Posted by r4874C
    2. there is an array consisting of text lines
    $lines = file ('entries.txt');
    how to reverse an array (make first line last, last first and so on)
    array_reverse ( array array [, bool preserve_keys] )


    I know I could have done a single thread, but I posted them as I found them

  5. #5
    loanskey's Avatar
    New User

    Status
    Offline
    Join Date
    Apr 2006
    Posts
    11

    Re: operations with files and arrays

    opening up a file in write mode will do that else for better understanding and examples please go to
    http://in2.php.net/manual/en/ref.filesystem.php
    and see the various function

    Ravish Kumar
    Last edited by HTML; 04-07-2006 at 05:56 AM.



Tags for this Thread