file_put_contents

(PHP 5, PHP 7)

file_put_contents锟斤拷一锟斤拷锟街凤拷锟斤拷写锟斤拷锟侥硷拷

说锟斤拷

file_put_contents ( string $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 (|) 锟斤拷锟斤拷锟斤拷锟斤拷械锟斤拷锟较★拷

Available flags
Flag 锟斤拷锟斤拷
FILE_USE_INCLUDE_PATH 锟斤拷 include 目录锟斤拷锟斤拷锟斤拷 filename锟斤拷 锟斤拷锟斤拷锟斤拷息锟缴参硷拷 include_path锟斤拷
FILE_APPEND 锟斤拷锟斤拷募锟� filename 锟窖撅拷锟斤拷锟节o拷追锟斤拷锟斤拷锟捷讹拷锟斤拷锟角革拷锟角★拷
LOCK_EX 锟斤拷写锟斤拷时锟斤拷锟揭伙拷锟斤拷锟秸硷拷锟斤拷锟�

context

一锟斤拷 context 锟斤拷源锟斤拷

锟斤拷锟斤拷值

锟矫猴拷锟斤拷锟斤拷锟斤拷锟斤拷写锟诫到锟侥硷拷锟斤拷锟斤拷锟捷碉拷锟街斤拷锟斤拷锟斤拷失锟斤拷时锟斤拷锟斤拷FALSE

Warning

锟剿猴拷锟斤拷锟斤拷锟杰凤拷锟截诧拷锟斤拷值 FALSE锟斤拷锟斤拷也锟斤拷锟杰凤拷锟截碉拷同锟斤拷 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$personFILE_APPEND LOCK_EX);
?>

锟斤拷锟斤拷锟斤拷志

锟芥本 说锟斤拷
5.0.0 Added context support
5.1.0 锟斤拷锟斤拷硕锟� LOCK_EX 锟斤拷支锟街猴拷 data 锟斤拷锟斤拷锟斤拷锟斤拷 stream 锟斤拷源锟侥癸拷锟杰★拷

注锟斤拷

Note: 锟剿猴拷锟斤拷锟缴帮拷全锟斤拷锟节讹拷锟斤拷锟狡讹拷锟斤拷

Tip

锟斤拷锟斤拷锟斤拷锟斤拷fopen 锟斤拷装锟斤拷锟斤拷锟节此猴拷锟斤拷锟叫o拷 URL 锟斤拷锟斤拷为锟侥硷拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟街革拷锟斤拷募锟斤拷锟斤拷锟斤拷 fopen()锟斤拷锟斤拷锟斤拷 wapper 锟侥诧拷同锟斤拷锟斤拷锟斤拷渭锟� 支锟街碉拷协锟斤拷头锟阶靶拷锟�锟斤拷注锟斤拷锟斤拷锟矫凤拷锟斤拷锟斤拷锟斤拷峁╋拷锟皆わ拷锟斤拷锟斤拷锟斤拷锟斤拷

锟轿硷拷

  • fopen() - 锟斤拷锟侥硷拷锟斤拷锟斤拷 URL
  • fwrite() - 写锟斤拷锟侥硷拷锟斤拷锟缴帮拷全锟斤拷锟节讹拷锟斤拷锟斤拷锟侥硷拷锟斤拷
  • file_get_contents() - 锟斤拷锟斤拷锟斤拷锟侥硷拷锟斤拷锟斤拷一锟斤拷锟街凤拷锟斤拷
  • stream_context_create() - 锟斤拷锟斤拷锟斤拷源锟斤拷锟斤拷锟斤拷锟斤拷