Save multiple variables to text file with php

Discussion in 'PHP' started by mcdeere02, Aug 5, 2009.

Thread Status:
Not open for further replies.
  1. #1
    I need to be able to save form data to a .txt file with php.

    I have three variables. $title, $description, and $content.

    I need to save them to the .txt file, and be able to open that .txt file with php and use all three of the variables.
     
    mcdeere02, Aug 5, 2009 IP
  2. EricBruggema

    EricBruggema Well-Known Member

    Messages:
    1,740
    Likes Received:
    28
    Best Answers:
    13
    Trophy Points:
    175
    #2
    EricBruggema, Aug 5, 2009 IP
  3. nomzz

    nomzz Well-Known Member

    Messages:
    475
    Likes Received:
    9
    Best Answers:
    0
    Trophy Points:
    110
    #3
    nomzz, Aug 5, 2009 IP
  4. premiumscripts

    premiumscripts Peon

    Messages:
    1,062
    Likes Received:
    48
    Best Answers:
    0
    Trophy Points:
    0
    #4
    
    filename = 'test.txt';
    $content = serialize(array('title' => $title, 'description' => $description, 'content' => $content));
    
    if (is_writable($filename)) {
        if (!$handle = fopen($filename, 'w')) {
             echo "Cannot open file ($filename)";
             exit;
        }
    
        if (fwrite($handle, $somecontent) === FALSE) {
            echo "Cannot write to file ($filename)";
            exit;
        }
    
        echo "Success, wrote to file ($filename)";
    
        fclose($handle);
    } else {
        echo "The file $filename is not writable";
    }
    
    PHP:
    To unserialize, you simply do:

    
    $arr = unserialize(file_get_contents($filename));
    $title = $arr['title']; //etc..
    
    PHP:
     
    premiumscripts, Aug 5, 2009 IP
  5. jazzwakf

    jazzwakf Peon

    Messages:
    4
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Its a simple task. You have to just write those variables to a txt file with delimiter like comma,tab etc. You can easily find code to write to a file. In case you need further help, you can get in touch with me.
     
    Last edited: Aug 6, 2009
    jazzwakf, Aug 6, 2009 IP
Thread Status:
Not open for further replies.