简体   繁体   中英

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..!

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM