|
-
Jul 14th, 2006, 09:22 AM
#1
Thread Starter
Member
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
-
Jul 14th, 2006, 01:26 PM
#2
Hyperactive Member
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
-
Jul 15th, 2006, 12:14 AM
#3
Re: Writing a file?
file_put_contents() does all the above at once.
-
Jul 15th, 2006, 01:02 AM
#4
Hyperactive Member
-
Jul 15th, 2006, 01:56 AM
#5
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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|