开发者

How to write a single dimension array to a file in php a.k.a. file_put_contents vs fopen+fwrite

开发者 https://www.devze.com 2022-12-12 14:23 出处:网络
Data: $data = array(\'Alice\', \'Bob\', \'Carol\', \'David\', \'Elizabeth\', \'Frank\'); Method A: file_put_contents(\'filename.ext\', implode(\"\\n\", $data) );

Data: $data = array('Alice', 'Bob', 'Carol', 'David', 'Elizabeth', 'Frank');


Method A:

file_put_contents('filename.ext', implode("\n", $data) );

Method 2:

$fp = fopen('filename.ext', 'w');
foreach($data as $name)
{
  fwrite($fp, $name . "\n");
}
fclose($fp);

Does one method have any significant penalties over the other?

Any significantly faster speed, eve开发者_如何学JAVAn at a cost? at no cost?

Preferences? Is it situational? Which would you use in production code vs 1-use throwaway scripts?

Note: Please ignore any issues of checking to see if the filename is writable, or filepointer is !false. assume 0 friction, and everything just "works".


Use serialize() rather than a half-baked imitation:

file_put_contents('filename.ext', serialize($data));

unless you need the file to be human readable and/or editable for whatever reason, in which case you need to carefully consider what data you're persisting to file so you can come up with a robust means of storing it.

As for file_put_contents() vs your loop, just do file_put_contents() unless you can't. It's less code and easier to read. I doubt there are any real differences with the implementation and even if they were, the performance of writing a file to disk is dwarfed by the fact that you're writing to disk. Don't sweat the small (irrelevant) stuff.


From the docs for file_put_contents:

This function is identical to calling fopen(), fwrite() and fclose() successively to write data to a file.

It would seem that constructing the string and writing it "at-once" would be more efficient in terms of I/O. Doing so would allow the data to be written in a large chunk, rather than in smaller bits. This is generally preferred when considering I/O performance.


file_put_contents() wins. fopen/write/close is just being verbose for no reason.

As Cletus says, just serialize the data with serialize().

Unless you're dealing with multibyte characters, which sometimes break serialize (but you can find various userland implementations of mb_serialize() online if you need that).

I'm not sure about the performance implications, but you might prefer storing your array as a json-serialized string (json_encode()). That way if some other system ever needs to read the file, you'll be using a standard serialization format, instead of a php-specific one. I seriously doubt there's any real performance difference one way or the other.


At a time of writing, this question is quite old, anyway.. If I understood it well, than all required is var_export($variable, true) within file_put_contents()

Example:

<?php

$filePathName = '/directory/file.php';

$array = array(
    0 => 'value',
    'key' => 
    array(
        'Yes' => true,
        'No' => false,
        'Whatever' => 'string', 
    ),
);

file_put_contents($filePathName, 
    '<?php $array = '.var_export($array, true).";\n", 
LOCK_EX);
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号