PHP文件处理

本文概述

PHP文件系统允许我们创建文件, 逐行读取文件, 逐字符读取文件, 写入文件, 追加文件, 删除文件和关闭文件。

PHP打开文件-fopen()

PHP fopen()函数用于打开文件。

句法

resource fopen ( string $filename , string $mode [, bool $use_include_path = false [, resource $context ]] )

例子

<?php
$handle = fopen("c:\\folder\\file.txt", "r");
?>

点击我了解更多详细信息…


PHP关闭文件-fclose()

PHP fclose()函数用于关闭打开的文件指针。

句法

ool fclose ( resource $handle )

例子

<?php
fclose($handle);
?>

PHP读取文件-fread()

PHP fread()函数用于读取文件的内容。它接受两个参数:资源和文件大小。

句法

string fread ( resource $handle , int $length )

例子

<?php  
$filename = "c:\\myfile.txt";  
$handle = fopen($filename, "r");//open file in read mode  

$contents = fread($handle, filesize($filename));//read file  

echo $contents;//printing data of file
fclose($handle);//close file  
?>

输出

hello php file

点击我了解更多详细信息…


PHP写入文件-fwrite()

PHP fwrite()函数用于将字符串的内容写入文件。

句法

int fwrite ( resource $handle , string $string [, int $length ] )

例子

<?php
$fp = fopen('data.txt', 'w');//open file in write mode
fwrite($fp, 'hello ');
fwrite($fp, 'php file');
fclose($fp);

echo "File written successfully";
?>

输出

File written successfully

点击我了解更多详细信息…


PHP删除文件-unlink()

PHP unlink()函数用于删除文件。

句法

bool unlink ( string $filename [, resource $context ] )

例子

<?php  
unlink('data.txt');
 
echo "File deleted successfully";
?>

点击我了解更多详细信息…

来源:

https://www.srcmini02.com/29189.html

微信公众号
手机浏览(小程序)

Warning: get_headers(): SSL operation failed with code 1. OpenSSL Error messages: error:14090086:SSL routines:ssl3_get_server_certificate:certificate verify failed in /mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line 57

Warning: get_headers(): Failed to enable crypto in /mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line 57

Warning: get_headers(https://static.shanhubei.com/qrcode/qrcode_viewid_38037.jpg): failed to open stream: operation failed in /mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line 57
0
分享到:
没有账号? 忘记密码?