Results 1 to 5 of 5

Thread: Writing a file?

  1. #1

    Thread Starter
    Member
    Join Date
    Jun 2003
    Posts
    52

    Writing a file?

    I'm stuck. I have this php code that basically loops through a file and puts it all in a variable. But is there a way to save that variables info to a new file or in an existing one???


    <?php

    $lines = file('storage.xml');
    $vars = "";

    foreach ($lines as $line_num => $line) {
    $vars .= htmlspecialchars($line) . "<br />\n";
    }

    echo $vars;
    ?>

    is there a way to put the contents of $vars in to a blank file (basically creating a new one) or an existing xml file???

    thanks, your help is much appreciated

  2. #2
    Hyperactive Member ..:RUDI:..'s Avatar
    Join Date
    Aug 2005
    Location
    Yorkshire, England! c0d: Da Vinci
    Posts
    344

    Re: Writing a file?

    PHP Code:
    $newfile "newfile.txt"// The file name.
    $filehand fopen($newfile,"a"); // Open the file with Writing Ability. (Will create the file if doesn't exist)
    fwrite($filehand$vars); // Write $vars to the file.
    fclose($filehand); // Close the file. 
    Rudi



    My Personal Home | MageBB Home | Elders Online
    Yes, Noteme is my father.

  3. #3
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: Writing a file?

    file_put_contents() does all the above at once.

  4. #4
    Hyperactive Member ..:RUDI:..'s Avatar
    Join Date
    Aug 2005
    Location
    Yorkshire, England! c0d: Da Vinci
    Posts
    344

    Re: Writing a file?

    Quote Originally Posted by penagate
    file_put_contents() does all the above at once.
    Damn you penagay!



    My Personal Home | MageBB Home | Elders Online
    Yes, Noteme is my father.

  5. #5
    VBA Nutter visualAd's Avatar
    Join Date
    Apr 2002
    Location
    Ickenham, UK
    Posts
    4,906

    Re: Writing a file?

    In PHP 5:
    PHP Code:
    file_put_contents('new_file'file_get_contents('old_file')); 
    In PHP 4:
    PHP Code:
    $hwnd fopen('new_file''wb');
    $data file_get_contents('old_file');

    fwrite($hwnd$data);
    fclose($hwnd); 
    Obviously, where necessary, you would check for errors in the above operations.
    PHP || MySql || Apache || Get Firefox || OpenOffice.org || Click || Slap ILMV || 1337 c0d || GotoMyPc For FREE! Part 1, Part 2

    | PHP Session --> Database Handler * Custom Error Handler * Installing PHP * HTML Form Handler * PHP 5 OOP * Using XML * Ajax * Xslt | VB6 Winsock - HTTP POST / GET * Winsock - HTTP File Upload

    Latest quote: crptcblade - VB6 executables can't be decompiled, only disassembled. And the disassembled code is even less useful than I am.

    Random VisualAd: Blog - Latest Post: When the Internet becomes Electricity!!


    Spread happiness and joy. Rate good posts.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width