html5教程-H5学习_番外篇_PHP数据库操作

发布时间:2018-12-14 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了html5教程-H5学习_番外篇_PHP数据库操作脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。
小宝典致力于为广大程序猿(媛)提供高品质的代码服务,请大家多多光顾小站,小宝典在此谢过。

1. 文件操作

1.1 打开关闭文件

foPEn()

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

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

例1:以只读方式打开一个位于本地服务器的文本文件

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

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

$fh = fopen("https://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. 课上练习代码

  右下角--> 开放权限 --> 改为可读可写         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('https://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); ?>

1. 文件操作

1.1 打开关闭文件

fopen()

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

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

例1:以只读方式打开一个位于本地服务器的文本文件

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

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

$fh = fopen("https://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. 课上练习代码

  右下角--> 开放权限 --> 改为可读可写         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('https://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); ?>

觉得可用,就经常来吧! 脚本宝典 欢迎评论哦! html5教程,巧夺天工,精雕玉琢。小宝典献丑了!

脚本宝典总结

以上是脚本宝典为你收集整理的html5教程-H5学习_番外篇_PHP数据库操作全部内容,希望文章能够帮你解决html5教程-H5学习_番外篇_PHP数据库操作所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。