(PHP 5, PHP 7)
file_put_contents — ��һ���ַ���д���ļ�
$filename
, mixed $data
[, int $flags
= 0
[, resource $context
]] ) : int�����ε��� fopen()��fwrite() �Լ� fclose() ����һ����
If filename
does not exist, the file is created.
Otherwise, the existing file is overwritten, unless the
FILE_APPEND
flag is set.
filename
Ҫ��д�����ݵ��ļ�����
data
Ҫд������ݡ����Ϳ����� string��array ������ stream ��Դ����������˵����������
��� data
ָ��Ϊ stream ��Դ������ stream
��������Ļ������ݽ���д�뵽ָ���ļ��У������÷���������ʹ��
stream_copy_to_stream() ������
���� data
���������飨������Ϊ��ά���飩������൱��
file_put_contents($filename, join('', $array))��
flags
flags
��ֵ������
���� flag ʹ�� OR (|) ��������е���ϡ�
Flag | ���� |
---|---|
FILE_USE_INCLUDE_PATH
|
�� include Ŀ¼������ filename ��
������Ϣ�ɲμ� include_path��
|
FILE_APPEND
|
����ļ� filename �Ѿ����ڣ������ݶ����Ǹ��ǡ�
|
LOCK_EX
|
��д��ʱ���һ����ռ���� |
context
һ�� context ��Դ��
�ú���������д�뵽�ļ������ݵ��ֽ�����ʧ��ʱ����FALSE
Example #1 Simple usage example
<?php
$file = 'people.txt';
// Open the file to get existing content
$current = file_get_contents($file);
// Append a new person to the file
$current .= "John Smith\n";
// Write the contents back to the file
file_put_contents($file, $current);
?>
Example #2 Using flags
<?php
$file = 'people.txt';
// The new person to add to the file
$person = "John Smith\n";
// Write the contents to the file,
// using the FILE_APPEND flag to append the content to the end of the file
// and the LOCK_EX flag to prevent anyone else writing to the file at the same time
file_put_contents($file, $person, FILE_APPEND | LOCK_EX);
?>
�汾 | ˵�� |
---|---|
5.0.0 | Added context support |
5.1.0 |
����˶� LOCK_EX ��֧�ֺ� data
�������� stream ��Դ�Ĺ��ܡ�
|
Note: �˺����ɰ�ȫ���ڶ����ƶ���
��������fopen ��װ�����ڴ˺����У� URL ����Ϊ�ļ������������ָ���ļ������ fopen()������ wapper �IJ�ͬ������μ� ֧�ֵ�Э��ͷ�װЭ����ע�����÷�������ṩ��Ԥ���������