JREAM Posted August 22, 2008 Share Posted August 22, 2008 I cant figure wyh this won't write? or read? I want to be able to write it line for line, i know how you read it line for line Is this possible? <?php // set file to write $file = 'dump.txt'; // open file $fh = fopen($file, 'w+') or die('Could not open file!'); // read file into array $data = file($fh) or die('Could not read file!'); foreach ($data as $line) { echo $line; // write to file fwrite($fh, "Look, Ma, I wrote a file! "); fwrite($fh, "123 "); fwrite($fh, "54 "); // close file } fclose($fh); ?> Link to comment https://forums.phpfreaks.com/topic/120943-fwrite-array-is-this-possible/ Share on other sites More sharing options...
akitchin Posted August 22, 2008 Share Posted August 22, 2008 you need a newline character to get a linebreak. try appending chr(10).chr(13) to the end of each line (those are a newline and a carriage return). what output or error, if any, are you getting from that code currently? Link to comment https://forums.phpfreaks.com/topic/120943-fwrite-array-is-this-possible/#findComment-623439 Share on other sites More sharing options...
PFMaBiSmAd Posted August 23, 2008 Share Posted August 23, 2008 The file() function takes a string file name. Passing it a file-handle from an fopen() function is meaningless. file (PHP 4, PHP 5) file — Reads entire file into an array Description array file ( string $filename [, int $flags [, resource $context]] ) Reads an entire file into an array. Note: You can use file_get_contents() to return the contents of a file as a string. Parameters filename Path to the file. Link to comment https://forums.phpfreaks.com/topic/120943-fwrite-array-is-this-possible/#findComment-623442 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.