tom smith
tom smith

Reputation: 1035

Writing large string to text file

A trivial use of PHP and frwite() to create/write to a text file.

However, is there a way to write a very large text string to a file using fwrite?()? I assume there is, and that it involves some form of buffer management. The PHP docs don't seem to have this covered.

Sample code:

$p = "Some really large string ~ 100-250K in size"
$myFile = "testp.txt";
$fh = fopen($myFile, 'w') or die("can't open file");
set_file_buffer($fh, 1000000);
fwrite($fh, $p);
fclose($fh);

Believe it or not, this simply gets a file with the name of the file inside the file. Using a much smaller text string, it works as expected. Pointers to what I should do would be useful.


UPDATE:

Some of you are missing that I did try the above with a string of ~100K, and it didn't work. All I got in the output file was the name of the file!!!

thanks

::: 2ND UPDATE....

 never mind.. the whole thing was user error... god i need a drink... or sleep!
 thanks

  php/fwrite works as i thought it would/should.. nothing to see here..!

Upvotes: 0

Views: 3648

Answers (1)

deceze
deceze

Reputation: 522016

There is no limit on how much data can be written to a stream (a file handle) in PHP and you do not need to fiddle with any buffers. Just write the data to the stream, done.

Upvotes: 1

Related Questions