h5加mysql加php_H5学习_番外篇_PHP数据库操作

0990d12dc8880306ad94644afb614761.png

1. 文件操作

1.1 打开关闭文件

fopen()

resource fopen ( string filename, string mode [, bool use_include_path [, resource zcontext]] )

fopen()函数将resource绑定到一个流或句柄。绑定之后。脚本就能够通过句柄与此资源交互;

例1:以仅仅读方式打开一个位于本地server的文本文件

$fh = fopen("test.txt", "r");

例2:以仅仅读方式打开一个远程文件

$fh = fopen("http://www.baidu.com", "r");

fclose()

bool fclose ( resource handle )

将 handle 指向的文件关闭 。假设成功则返回 TRUE。失败则返回 FALSE;

文件指针必须有效,而且是通过 fopen() 或 fsockopen() 成功打开的;

尽管每一个请求最后都会自己主动关闭文件。但明白的关闭打开的全部文件是一个好的习惯;

例:

$fh = fopen("test.txt", "r");

fclose($fh);

1.2 读取文件

php 提供了非常多从文件里读取数据的方法,不仅能够一次仅仅读取一个字符。还能够一次读取整个文件。

fread()

string fread ( int handle, int length )

fread()函数从handle指定的资源中读取length个字符,

当到达EOF或读取到length个字符时读取将停止。

假设要读取整个文件,使用filesize()函数确定应该读取的字符数;

例:

$file = "test.txt";

$fh = fopen( $file, "r");

$str = fread($fh, filesize($file));

echo $str;

fclose($fh);

fgets()

string fgets ( int handle [, int length] )

fgets()函数从handle指定的资源中读取一行字符。碰到换行符(包含在返回值中)、

EOF 或者已经读取了 length - 1 字节后停止(看先碰到那一种情况);

例:

逐行读取文件

$handle = fopen("data.txt", "r");

while(!feof($handle)){

$content = fgets($handle);

$content= iconv('gbk','utf-8',$content);

echo $content."
”;

}

fclose($handle);

注意:假设没有指定 length。则默觉得 1K,或者说 1024 字节。

file()

array file ( string $filename [, int $flags = 0 [, resource $context ]])

file()函数将文件读取到数组中。各元素由换行符分隔。

例:

$arr = file("test.txt");

print_r($arr);

file_get_contents()

string file_get_contents ( string filename [, bool use_include_path [, resource context [, int offset [, int maxlen]]]] )

file_get_contents()函数将文件内容读到字符串中;

例:

$str = file_get_contents("test.txt");

echo $str;

1.3 写入文件

fwrite()

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

fwrite()函数将string的内容写入到由handle指定的资源中。

假设指定length參数。将在写入Length个字符时停止。

例:

$str = "test text";

$fh = fopen("test.txt", "a");

fwrite($fh, $str);

fclose($fh);

file_put_contents()

int file_put_contents ( string filename, string data [, int flags [, resource context]] )

file_put_contents()函数将一个字符串写入文件。与依次调用fopen(),fwrite(),fclose()功能一样;

例:

$str = "hello";

file_put_contents("test.txt", $str);

1.4 复制,重命名,删除文件

copy()

bool copy ( string source, string dest )

将文件从 source 复制到 dest。假设成功则返回 TRUE,失败则返回 FALSE。

例:Copy("test.txt", "test.txt.bak");

rename()

bool rename ( string oldname, string newname [, resource context] )

尝试把 oldname 重命名为 newname。 假设成 功则返回 TRUE,失败则返回 FALSE。

例:rename("test.txt", “test2.txt”);

unlink()

bool unlink ( string filename )

删除文件,假设删除成功返回true, 否则返回false;

例1:

删除一个文本文件

unlink(“test.txt")。

1.5 读取文件夹

copy()

bool copy ( string source, string dest )

将文件从 source 复制到 dest。

假设成功则返回 TRUE,失败则返回 FALSE。

例:Copy("test.txt", "test.txt.bak");

rename()

bool rename ( string oldname, string newname [, resource context] )

尝试把 oldname 重命名为 newname。 假设成功则返回 TRUE,失败则返回 FALSE。

例:rename("test.txt", “test2.txt”);

unlink()

bool unlink ( string filename )

删除文件。假设删除成功返回true, 否则返回false;

例1:

删除一个文本文件

unlink(“test.txt")。

scandir()

array scandir ( string directory [, int sorting_order [, resource context]] )

返回一个包含有 directory 中的文件和文件夹的数组;

rmdir()

bool rmdir ( string dirname )

删除文件夹

mkdir()

bool mkdir ( string pathname [, int mode [, bool recursive [, resource context]]] )

尝试新建一个由 pathname 指定的文件夹。

1.6 其它文件操作函数

filesize()

int filesize ( string filename )

取得文件的大小,以字节为单位

filectime()

int filectime ( string filename )

取得文件的创建时间,以unix时间戳方式返回

例:

$t = filectime("test.txt");

echo date("Y-m-d H:i:s", $t);

fileatime() 返回文件的最后改变时间;

filemtime() 返回文件的最后改动时间;

注:”最后改变时间”不同于 “最后改动时间”。

最后改变时间指的是对文件inode数据的不论什么改变。包含改变权限。所属组。拥有者等; 而最后改动时间指的是对文件内容的改动

file_exists() 检查文件或文件夹是否存在,假设存在返回true, 否则返回false;

is_readable() 推断文件是否可读,假设文件存在而且可读,则返回true;

is_writable() 推断文件是否可写。假设文件存在而且可写,则返回true;

1.7 解析文件夹路径函数

basename()

string basename ( string path [, string suffix] )

返回路径中的文件名称部份,当指定了可选參数suffix会将这部分内容去掉;

例:

2. 课上练习代码

php

//打开文件

$rh = fopen('PHP_3.txt', 'r+');

//读取文件,第一个參数是文件句柄,第二个是读取方式

//计算文件大小(字节)

$num = filesize('PHP_3.txt');

$str = fread($rh, $num);

echo $str;

//假设设置文件訪问错误,须要去更改文件的权限,属性 --> 右下角--> 开放权限 --> 改为可读可写

echo "


";

//换行读取 识别 enter 不识别

$str_1 = fgets($rh);

$str_2 = fgets($rh);

//换行读取再次读取还会继续上次的读取位置继续读取

echo $str_1;

echo "


";

echo $str_2;

//file 将文件内容转化为数组,
直接转化为换行,回车作为分隔符

$arr = file('PHP_3.txt');

print_r($arr);

echo "


";

//file_get_contents 读取文件内容。返回字符串。而且能够读取外部网络数据

// echo file_get_contents('PHP_3.txt');

//直接读取站点,存到一个文本中,能够直接获取对方的页面静态布局。注意,是静态的!

// $str_3 = file_get_contents('http://www.lanou3g.com');

// file_put_contents('PHP_3.txt', $str_3);

//重命名

// rename('PHP_3.txt', '1.txt');

// rename('1.txt','PHP_3.txt');

//文件拷贝 使用../ 替代上级文件夹

// copy('PHP_3.txt', '../test.txt');

//读取文件夹

//1.打开文件文件夹句柄 .(一个点) 获取本级文件夹 ..(两个点)是上级文件夹

$rh_1 = opendir('.');

// $arr = readdir()

//readdir 获取文件文件夹,这个和 MySQL 一样。必须使用循环取出

while ($num = readdir($rh_1)) {

//读取出来的

echo $num;

echo "


";

}

//读取文件夹

print_r(scandir('.'));

//创建一个新的文件夹

// mkdir('asdasd');

//删除整个文件夹 删除文件夹必须保证文件夹内部没有其它文件

// $is_bool = rmdir('1');

//删除

// unlink('PHP_3.txt');

//获取文件创建时间

echo filectime('PHP_3.txt');

echo "


";

//返回文件最后訪问的时间

echo fileatime('PHP_3.txt');

echo "


";

//解析文件详细名称

echo basename('PHP_3.txt','txt');

echo "


";

//获取当前文件所在的文件夹的名称

echo dirname('file/PHP_3.txt');

echo "


";

//返回全程,拓展名,文件名称

print_r(pathinfo("PHP_3.txt"));

//改动文件文件夹权限

echo "


";

fclose($rh);

fclose($rh_1);

?

>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值