this is demo.txt 1
this is demo.txt 2
this is demo.txt 3
this is demo.txt 4
this is demo.txt 5
this is demo.txt 6
this is demo.txt 7
this is demo.txt 8
this is demo.txt 9
fopen ( string $filename , string $mode [, bool $use_include_path = false [, resource $context ]] ) : resource
$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:[email protected]/somefile.txt", "w");
$fp = fsockopen("www.example.com", 80, $errno, $errstr, 30);
if (!$fp) {
echo "$errstr ($errno)
\n";
} else {
$out = "GET / HTTP/1.1\r\n";
$out .= "Host: www.example.com\r\n";
$out .= "Connection: Close\r\n\r\n";
fwrite($fp, $out);
while (!feof($fp)) {
echo fgets($fp, 128);
}
fclose($fp);
}
$fp = fsockopen("udp://127.0.0.1", 13, $errno, $errstr);
if (!$fp) {
echo "ERROR: $errno - $errstr
\n";
} else {
fwrite($fp, "\n");
echo fread($fp, 26);
fclose($fp);
}
fwrite ( resource $handle , string $string [, int $length ] ) : int
$file = fopen("test.txt","w");
echo fwrite($file,"Hello World. Testing!");
fclose($file);
echo "1) ".basename("/etc/sudoers.d").PHP_EOL;
echo "2) ".basename("/etc/sudoers.d", ".d").PHP_EOL;
echo "3) ".basename("/etc/passwd").PHP_EOL;
echo "4) ".basename("/etc/").PHP_EOL;
echo "5) ".basename(".").PHP_EOL;
echo "6) ".basename("/");
/*
1) sudoers.d
2) sudoers
3) passwd
4) etc
5) .
6)
*/
copy ( string $source , string $dest [, resource $context ] ) : bool
echo copy("要复制的文件路径/链接.txt","target.txt");//bool(true/false)
dirname ( string $path ) : string
echo "1) " . dirname("/etc/passwd") . PHP_EOL; // 1) /etc
echo "2) " . dirname("/etc/") . PHP_EOL; // 2) / (or \ on Windows)
echo "3) " . dirname("."); // 3) .
disk_free_space ( string $directory ) : float
echo disk_free_space('c:');//37684822016 返回可用的字节数。
disk_total_space ( string $directory ) : float
echo disk_total_space('c:');//118409187328 字节数
fclose ( resource $handle ) : bool
$handle = fopen('somefile.txt', 'r');
fclose($handle);
feof ( resource $handle ) : bool
$file = fopen("demo.txt", "r");
//输出文本中所有的行,直到文件结束为止。
while(!feof($file))
{
echo fgets($file);
}
fclose($file);
fgetc ( resource $handle ) : string
$file = fopen("demo.txt", "r");
echo fgetc($file);//t
fgets ( resource $handle [, int $length ] ) : string
$file = fopen("demo.txt", "r");
echo fgets($file,2048);//this is demo.txt 1
fgetss ( resource $handle [, int $length [, string $allowable_tags ]] ) : string
demo.txt
<div><p><span>this is demo.txt 1</span></p></div>
demo.php
$file = fopen("demo.txt", "r");
echo fgetss($file,1024,''
);
//this is demo.txt 1
file ( string $filename [, int $flags = 0 [, resource $context ]] ) : array
demo.txt
<div><p><span>this is demo.txt 1</span></p></div>
this is demo.txt 2
this is demo.txt 3
this is demo.txt 4
this is demo.txt 5
this is demo.txt 6
this is demo.txt 7
this is demo.txt 8
this is demo.txt 9
print_r( file('demo.txt',FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES));
/*
Array
(
[0] => this is demo.txt 1
[1] => this is demo.txt 2
[2] => this is demo.txt 3
[3] => this is demo.txt 4
[4] => this is demo.txt 5
[5] => this is demo.txt 6
[6] => this is demo.txt 7
[7] => this is demo.txt 8
[8] => this is demo.txt 9
)*/
file_get_contents ( string $filename [, bool $use_include_path = false [, resource $context [, int $offset = -1 [, int $maxlen ]]]] ) : string
echo file_get_contents('demo.txt');
/*
this is demo.txt 1
this is demo.txt 2
this is demo.txt 3
this is demo.txt 4
this is demo.txt 5
this is demo.txt 6
this is demo.txt 7
this is demo.txt 8
this is demo.txt 9
*/
echo file_get_contents('demo.txt',false,null,1,10);//div>
file_put_contents ( string $filename , mixed $data [, int $flags = 0 [, resource $context ]] ) : int
该函数将返回写入到文件内数据的字节数,失败时返回FALSE
echo file_put_contents('demo.txt','this is yzx',FILE_APPEND);
flock( resource $handle , int $operation [, int &$wouldblock ] ) : bool
$file= fopen("demo.txt", "r+");
// 排它性的锁定
if (flock($file,LOCK_EX ))
{
fwrite($file,"Write something fsdfsadf");
// release lock
flock($file,LOCK_UN);
echo 111;
}
else
{
echo "Error locking file!";
}
fclose($file);
若成功,则返回 true,失败则返回 false。
fgetcsv ( resource $handle [, int $length = 0 [, string $delimiter = ',' [, string $enclosure = '"' [, string $escape = '\\' ]]]] ) : array
$file = fopen("demo.csv","r");
print_r(fgetcsv($file)) ;
fclose($file);die;
fputcsv ( resource $handle , array $fields [, string $delimiter = ',' [, string $enclosure = '"' ]] ) : int
$list = array (
array('aaa', 'bbb', 'ccc', 'dddd'),
array('123', '456', '789'),
array('"aaa"', '"bbb"')
);
$fp = fopen('file.csv', 'w');
foreach ($list as $fields) {
fputcsv($fp, $fields);
}
fclose($fp);
$file = fopen("demo.txt","r");
// 读取第一行
fgets($file);
// 把文件的其余部分发送到输出缓存
echo fpassthru($file);
fclose($file);
test.ini
[names]
me = Robert
you = Peter
[urls]
first = "http://www.example.com"
second = "http://www.w3school.com.cn"
demo.php
print_r(parse_ini_file("test.ini"));
/*
Array
(
[me] => Robert
[you] => Peter
[first] => http://www.example.com
[second] => http://www.w3school.com.cn
)
*/
print_r(parse_ini_file("test.ini",true));
Array
(
[names] => Array
(
[me] => Robert
[you] => Peter
)
[urls] => Array
(
[first] => http://www.example.com
[second] => http://www.w3school.com.cn
)
)
var_dump(pathinfo('http://blog.yaozhixiong.top/php/index.php'));
array(4) {
'dirname' =>
string(31) "http://blog.yaozhixiong.top/php"
'basename' =>
string(9) "index.php"
'extension' =>
string(3) "php"
'filename' =>
string(5) "index"
}
只能删除空的目录 且不能递归删除
若成功,则返回 true,失败则返回 false。
$file = fopen("demo.txt","r");
fseek($file,3);
// 读取第一行
echo fgets($file);//v>this is demo.txt 1
$file = fopen("demo.txt","r");
fseek($file,3);
// 读取第一行
echo fgets($file);//v>this is demo.txt 1
this is demo.txt 1
若成功,则返回 true。若失败,则返回 false。
rint_r(glob("*.txt"));
Array
(
[0] => target.txt
[1] => source.txt
[2] => test.txt
[3] => test2.txt
)
scandir ( string $directory [, int $sorting_order [, resource $context ]] ) : array
$numdir = $numfile = 0;
function foo ($dir)
{
global $numdir,$numfile;
foreach (scandir($dir) as $key => $val)
{
if ($val !== '.' && $val !== '..')
{
if ( is_dir($file = $dir.'/'.$val))
{
++$numdir;//目录数
foo($file);
}
if ( is_file($dir.'/'.$val))
{
++$numfile;//文件数量
}
}
}
}
foo('laravel');
echo '目录数:'.$numdir.PHP_EOL;
echo '文件数:'.$numfile;