(PHP 4, PHP 5, PHP 7)
fopen — 锟斤拷锟侥硷拷锟斤拷锟斤拷 URL
$filename
, string $mode
[, bool $use_include_path
= false
[, resource $context
]] ) : resource
fopen() 锟斤拷
filename
指锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷源锟襟定碉拷一锟斤拷锟斤拷锟较★拷
filename
锟斤拷锟�
filename
锟斤拷 "scheme://..." 锟侥革拷式锟斤拷锟津被碉拷锟斤拷一锟斤拷
URL锟斤拷PHP 锟斤拷锟斤拷锟斤拷协锟介处锟斤拷锟斤拷锟斤拷也锟斤拷锟斤拷为锟斤拷装协锟介)锟斤拷锟斤拷锟斤拷锟侥J斤拷锟斤拷锟斤拷锟斤拷协锟斤拷锟斤拷未注锟斤拷锟阶靶拷椋琍HP
锟斤拷锟斤拷锟斤拷一锟斤拷锟斤拷息锟斤拷锟斤拷锟斤拷锟斤拷锟脚憋拷锟斤拷潜锟节碉拷锟斤拷锟解并锟斤拷
filename
锟斤拷锟斤拷一锟斤拷锟斤拷通锟斤拷锟侥硷拷锟斤拷锟斤拷锟斤拷执锟斤拷锟斤拷去锟斤拷
锟斤拷锟� PHP 锟斤拷为
filename
指锟斤拷锟斤拷锟斤拷一锟斤拷锟斤拷锟斤拷锟侥硷拷锟斤拷锟斤拷锟斤拷锟斤拷锟节革拷锟侥硷拷锟较达拷一锟斤拷锟斤拷锟斤拷锟斤拷锟侥硷拷锟斤拷锟斤拷锟斤拷
PHP 锟斤拷锟皆凤拷锟绞的o拷锟斤拷锟斤拷锟揭凤拷锟斤拷募锟斤拷锟斤拷锟饺拷锟斤拷锟斤拷锟矫凤拷锟绞★拷锟斤拷锟斤拷锟斤拷锟斤拷锟�锟斤拷全模式锟斤拷锟斤拷
open_basedir
锟斤拷锟接︼拷媒锟揭伙拷锟斤拷锟斤拷锟斤拷啤锟�
锟斤拷锟� PHP 锟斤拷为
filename
指锟斤拷锟斤拷锟斤拷一锟斤拷锟斤拷注锟斤拷锟叫拷椋拷锟斤拷锟叫拷楸蛔拷锟轿伙拷锟斤拷锟斤拷锟�
URL锟斤拷PHP 锟斤拷锟斤拷椴⑷凤拷锟�
allow_url_fopen
锟窖憋拷锟斤拷锟筋。锟斤拷锟斤拷乇锟斤拷耍锟絇HP 锟斤拷锟斤拷锟斤拷一锟斤拷锟斤拷锟芥,锟斤拷 fopen 锟侥碉拷锟斤拷锟斤拷失锟杰★拷
Note:
锟斤拷支锟街碉拷协锟斤拷锟叫憋拷锟�支锟街碉拷协锟斤拷头锟阶靶拷锟�锟斤拷某些协锟介(也锟斤拷锟斤拷为 wrappers锟斤拷支锟斤拷 context 锟酵o拷锟斤拷 php.ini 选锟筋。锟轿硷拷锟斤拷应锟斤拷页锟斤拷锟斤拷些选锟斤拷锟斤拷员锟斤拷瓒拷锟斤拷锟斤拷锟� php.ini 锟斤拷锟斤拷锟斤拷 http wrapper 锟斤拷 user_agent 值锟斤拷锟斤拷
On the Windows platform, be careful to escape any backslashes used in the path to the file, or use forward slashes.
<?php
$handle = fopen("c:\\folder\\resource.txt", "r");
?>
mode
mode
锟斤拷锟斤拷指锟斤拷锟斤拷锟斤拷要锟襟到革拷锟斤拷锟侥凤拷锟斤拷锟斤拷锟酵★拷锟斤拷锟斤拷锟斤拷锟斤拷锟铰o拷
mode |
说锟斤拷 |
---|---|
'r' | 只锟斤拷锟斤拷式锟津开o拷锟斤拷锟侥硷拷指锟斤拷指锟斤拷锟侥硷拷头锟斤拷 |
'r+' | 锟斤拷写锟斤拷式锟津开o拷锟斤拷锟侥硷拷指锟斤拷指锟斤拷锟侥硷拷头锟斤拷 |
'w' | 写锟诫方式锟津开o拷锟斤拷锟侥硷拷指锟斤拷指锟斤拷锟侥硷拷头锟斤拷锟斤拷锟侥硷拷锟斤拷小锟斤拷为锟姐。锟斤拷锟斤拷募锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷源锟斤拷锟街拷锟� |
'w+' | 锟斤拷写锟斤拷式锟津开o拷锟斤拷锟侥硷拷指锟斤拷指锟斤拷锟侥硷拷头锟斤拷锟斤拷锟侥硷拷锟斤拷小锟斤拷为锟姐。锟斤拷锟斤拷募锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷源锟斤拷锟街拷锟� |
'a' | 写锟诫方式锟津开o拷锟斤拷锟侥硷拷指锟斤拷指锟斤拷锟侥硷拷末尾锟斤拷锟斤拷锟斤拷募锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷源锟斤拷锟街拷锟� |
'a+' | 锟斤拷写锟斤拷式锟津开o拷锟斤拷锟侥硷拷指锟斤拷指锟斤拷锟侥硷拷末尾锟斤拷锟斤拷锟斤拷募锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷源锟斤拷锟街拷锟� |
'x' |
锟斤拷锟斤拷锟斤拷锟斤拷写锟诫方式锟津开o拷锟斤拷锟侥硷拷指锟斤拷指锟斤拷锟侥硷拷头锟斤拷锟斤拷锟斤拷募锟斤拷汛锟斤拷冢锟斤拷锟�
fopen() 锟斤拷锟斤拷失锟杰诧拷锟斤拷锟斤拷 FALSE 锟斤拷锟斤拷锟斤拷锟斤拷一锟斤拷
E_WARNING 锟斤拷锟斤拷拇锟斤拷锟斤拷锟较拷锟斤拷锟斤拷锟侥硷拷锟斤拷锟斤拷锟斤拷锟斤拷锟皆达拷锟斤拷之锟斤拷锟斤拷透锟�
锟阶诧拷锟� open(2) 系统锟斤拷锟斤拷指锟斤拷
O_EXCL|O_CREAT 锟斤拷锟斤拷堑燃鄣摹锟�
|
'x+' | 锟斤拷锟斤拷锟斤拷锟皆讹拷写锟斤拷式锟津开o拷锟斤拷锟斤拷锟斤拷锟斤拷为锟斤拷 'x' 一锟斤拷锟斤拷 |
'c' | Open the file for writing only. If the file does not exist, it is created. If it exists, it is neither truncated (as opposed to 'w'), nor the call to this function fails (as is the case with 'x'). The file pointer is positioned on the beginning of the file. This may be useful if it's desired to get an advisory lock (see flock()) before attempting to modify the file, as using 'w' could truncate the file before the lock was obtained (if truncation is desired, ftruncate() can be used after the lock is requested). |
'c+' | Open the file for reading and writing; otherwise it has the same behavior as 'c'. |
Note:
锟斤拷同锟侥诧拷锟斤拷系统锟斤拷锟斤拷锟斤拷胁锟酵拷锟斤拷薪锟斤拷锟较帮拷摺锟斤拷锟叫达拷锟揭伙拷锟斤拷谋锟斤拷募锟斤拷锟斤拷锟斤拷锟斤拷一锟斤拷锟斤拷锟斤拷时锟斤拷锟斤拷要使锟矫凤拷锟较诧拷锟斤拷系统锟斤拷锟叫斤拷锟斤拷锟斤拷锟脚★拷锟斤拷锟斤拷 Unix 锟斤拷系统使锟斤拷 \n 锟斤拷为锟叫斤拷锟斤拷锟街凤拷锟斤拷锟斤拷锟斤拷 Windows 锟斤拷系统使锟斤拷 \r\n 锟斤拷为锟叫斤拷锟斤拷锟街凤拷锟斤拷锟斤拷锟斤拷 Macintosh 锟斤拷系统使锟斤拷 \r 锟斤拷为锟叫斤拷锟斤拷锟街凤拷锟斤拷
锟斤拷锟叫达拷锟斤拷募锟绞笔癸拷锟斤拷舜锟斤拷锟斤拷锟叫斤拷锟斤拷锟斤拷锟脚o拷锟斤拷锟斤拷锟斤拷应锟矫筹拷锟斤拷锟斤拷锟叫╋拷募锟绞憋拷锟斤拷芑锟斤拷锟街得很癸拷锟届。
Windows 锟斤拷锟结供锟斤拷一锟斤拷锟侥憋拷转锟斤拷锟斤拷牵锟�'t'锟斤拷锟斤拷锟斤拷透锟斤拷锟截斤拷 \n 转锟斤拷为 \r\n锟斤拷锟斤拷硕锟接︼拷锟斤拷锟斤拷锟绞癸拷锟� 'b' 锟斤拷强锟斤拷使锟矫讹拷锟斤拷锟斤拷模式锟斤拷锟斤拷锟斤拷锟酵诧拷锟斤拷转锟斤拷锟斤拷锟捷★拷要使锟斤拷锟斤拷些锟斤拷牵锟揭达拷锟� 'b' 锟斤拷锟斤拷锟斤拷 't' 锟斤拷为
mode
锟斤拷锟斤拷锟斤拷锟斤拷锟揭伙拷锟斤拷址锟斤拷锟�默锟较碉拷转锟斤拷模式锟斤拷锟斤拷锟斤拷 SAPI 锟斤拷锟斤拷使锟矫碉拷 PHP 锟芥本锟斤拷锟斤拷锟轿拷吮锟斤拷锟斤拷锟街诧拷锟斤拷锟斤拷锟斤拷锟街革拷锟角★拷锟斤拷谋锟角★拷锟斤拷锟斤拷遣锟斤拷锟斤拷锟斤拷谋锟斤拷募锟斤拷锟斤拷诮疟锟斤拷锟绞癸拷锟斤拷锟� \n 锟斤拷为锟叫斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷要锟斤拷锟斤拷锟斤拷些锟侥硷拷锟斤拷锟皆憋拷锟斤拷锟斤拷应锟矫筹拷锟斤拷锟斤拷锟斤拷 Notepad 锟斤拷取锟斤拷锟斤拷锟斤拷 mode 锟斤拷使锟斤拷 't'锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟绞癸拷锟� 'b'锟斤拷
锟节诧拷锟斤拷锟斤拷锟斤拷锟斤拷锟侥硷拷时锟斤拷锟矫伙拷锟街革拷锟� 'b' 锟斤拷牵锟斤拷锟斤拷芑锟斤拷锟斤拷锟揭恍╋拷锟街碉拷锟斤拷锟解,锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷图片锟侥硷拷锟皆硷拷锟斤拷锟斤拷 \r\n 锟街凤拷锟斤拷锟斤拷锟斤拷锟斤拷狻�
Note:
为锟斤拷植锟皆匡拷锟角o拷强锟揭斤拷锟斤拷锟斤拷锟斤拷 fopen() 锟斤拷锟侥硷拷时锟斤拷锟斤拷使锟斤拷 'b' 锟斤拷恰锟�
Note:
锟斤拷一锟轿o拷为锟斤拷植锟皆匡拷锟角o拷强锟揭斤拷锟斤拷锟斤拷锟斤拷写锟斤拷些锟斤拷锟斤拷锟斤拷 't' 模式锟侥达拷锟斤拷使锟斤拷使锟斤拷锟斤拷确锟斤拷锟叫斤拷锟斤拷锟斤拷锟斤拷锟侥筹拷 'b' 模式锟斤拷
use_include_path
锟斤拷锟揭诧拷锟揭拷锟�
include_path
锟斤拷锟斤拷寻锟侥硷拷锟侥伙拷锟斤拷锟斤拷锟皆斤拷锟斤拷选锟侥碉拷锟斤拷锟斤拷锟斤拷锟斤拷
use_include_path
锟斤拷为 '1' 锟斤拷 TRUE
锟斤拷
context
Note: 锟斤拷 PHP 5.0.0 锟斤拷锟斤拷锟斤拷锟剿讹拷锟斤拷锟斤拷锟侥o拷Context锟斤拷锟斤拷支锟街★拷锟叫癸拷锟斤拷锟斤拷锟侥o拷Context锟斤拷锟斤拷说锟斤拷锟轿硷拷 Streams锟斤拷
锟缴癸拷时锟斤拷锟斤拷锟侥硷拷指锟斤拷锟斤拷源锟斤拷锟斤拷锟斤拷锟绞э拷埽锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟� FALSE
锟斤拷
锟斤拷锟斤拷锟绞э拷埽锟斤拷锟斤拷锟斤拷一锟斤拷 E_WARNING
锟斤拷锟襟。匡拷锟斤拷通锟斤拷 @ 锟斤拷锟斤拷锟轿达拷锟斤拷
锟芥本 | 说锟斤拷 |
---|---|
4.3.2 | 锟斤拷 PHP 4.3.2 锟金,讹拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷坪锟斤拷谋锟侥J斤拷锟狡教拷锟侥J斤拷锟斤拷锟斤拷锟轿拷锟斤拷锟斤拷锟侥J斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷疟锟斤拷锟斤拷锟斤拷锟斤拷猓拷锟斤拷锟斤拷锟绞笔癸拷锟� 't' 锟斤拷牵锟街憋拷锟斤拷锟斤拷械慕疟锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟剿碉拷母锟轿拷锟斤拷锟斤拷锟街诧拷锟斤拷院锟� |
4.3.2 | 锟斤拷锟斤拷锟斤拷选锟斤拷 'x' 锟斤拷 'x+' |
5.2.6 | 锟斤拷锟斤拷锟斤拷选锟斤拷 'c' 锟斤拷 'c+' |
Example #1 fopen() 锟斤拷锟斤拷
<?php
$handle = fopen("/home/rasmus/file.txt", "r");
$handle = fopen("/home/rasmus/file.gif", "wb");
$handle = fopen("http://www.example.com/", "r");
$handle = fopen("ftp://user:password@example.com/somefile.txt", "w");
?>
使锟斤拷 SSL 时锟斤拷Microsoft IIS 锟斤拷违锟斤拷协锟介不锟斤拷锟斤拷close_notify锟斤拷蔷凸乇锟斤拷锟斤拷印锟絇HP 锟斤拷锟节碉拷锟斤拷锟斤拷锟斤拷尾锟斤拷时锟斤拷锟斤拷"SSL: Fatal Protocol Error"锟斤拷 要锟斤拷锟斤拷锟斤拷锟斤拷猓�error_reporting 应锟借定为锟斤拷锟酵硷拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟芥。 PHP 4.3.7 锟斤拷锟斤拷锟竭版本锟斤拷锟斤拷锟斤拷使锟斤拷 https:// 锟斤拷装锟斤拷锟斤拷锟斤拷时锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟� IIS 锟斤拷锟斤拷锟斤拷锟斤拷锟� 锟斤拷锟斤拷锟狡撅拷锟芥。锟斤拷使锟斤拷 fsockopen() 锟斤拷锟斤拷 ssl:// 锟阶斤拷锟斤拷时, 锟斤拷锟斤拷锟斤拷锟斤拷锟解并锟斤拷锟狡此撅拷锟芥。
Note: 锟斤拷锟斤拷锟斤拷 锟斤拷全模式时锟斤拷 PHP 锟斤拷锟斤拷执锟叫脚憋拷时锟斤拷楸伙拷疟锟斤拷锟斤拷锟斤拷锟侥柯硷拷欠锟斤拷氡恢达拷械慕疟锟斤拷锟斤拷锟酵拷锟� UID锟斤拷锟斤拷锟斤拷锟竭o拷锟斤拷
Note:
锟斤拷锟斤拷锟斤拷梅锟斤拷锟斤拷锟侥o拷锟芥本锟斤拷 PHP 时锟节打开猴拷写锟斤拷锟侥硷拷锟斤拷锟斤拷锟斤拷锟斤拷锟解,锟斤拷住要确锟斤拷锟斤拷使锟矫碉拷锟侥硷拷锟斤拷目录锟角凤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟杰癸拷锟斤拷锟绞的★拷
Note:
This function may also succeed when
filename
is a directory. If you are unsure whetherfilename
is a file or a directory, you may need to use the is_dir() function before calling fopen().