H5学习_番外篇_PHP数据库操作

这里写图片描述

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("http://www.baidu.com", "r");

  2. fclose()

    bool fclose ( resource handle )

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

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

    虽然每个请求最后都会自动关闭文件,但明确的关闭打开的所有文件是一个好的习惯;

    例:

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

1.2 读取文件

php 提供了很多从文件中读取数据的方法,不仅可以一次只读取一个字符,还可以一次读取整个文件。

  1. 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);
  2. 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."<br />”;
    }   
    fclose($handle);

    注意:如果没有指定 length,则默认为 1K,或者说 1024 字节。

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

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

    $arr = file("test.txt");
    print_r($arr);
  4. 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 写入文件

  1. 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);
  2. 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 复制,重命名,删除文件

  1. copy()

    bool copy ( string source, string dest )

    将文件从 source 拷贝到 dest。如果成功则返回 TRUE,失败则返回 FALSE。

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

  2. rename()

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

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

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

  3. unlink()

    bool unlink ( string filename )

    删除文件,如果删除成功返回true, 否则返回false;

    例1:

    删除一个文本文件
    unlink(“test.txt");

1.5 读取目录

  1. copy()

    bool copy ( string source, string dest )

    将文件从 source 拷贝到 dest。如果成功则返回 TRUE,失败则返回 FALSE。

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

  2. rename()

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

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

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

  3. unlink()

    bool unlink ( string filename )

    删除文件,如果删除成功返回true, 否则返回false;

    例1:

    删除一个文本文件
    unlink(“test.txt");     
  4. scandir()

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

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

  5. rmdir()

    bool rmdir ( string dirname )

    删除目录

  6. mkdir()

    bool mkdir ( string pathname [, int mode [, bool recursive [, resource context]]] )
    
尝试新建一个由 pathname 指定的目录。

1.6 其他文件操作函数

  1. filesize()

    int filesize ( string filename )

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

  2. filectime()

    int filectime ( string filename )

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

    例:

    $t = filectime("test.txt");
    echo date("Y-m-d H:i:s", $t);
  3. fileatime() 返回文件的最后改变时间;

  4. filemtime() 返回文件的最后修改时间;

    注:”最后改变时间”不同于 “最后修改时间”。最后改变时间指的是对文件inode数据的任何改变,包括改变权限,所属组,拥有者等; 而最后修改时间指的是对文件内容的修改

  5. file_exists() 检查文件或目录是否存在,如果存在返回true, 否则返回false;

  6. is_readable() 判断文件是否可读,如果文件存在并且可读,则返回true;

  7. is_writable() 判断文件是否可写,如果文件存在并且可写,则返回true;

1.7 解析目录路径函数

  1. 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 "<hr>";

        //换行读取  识别 enter 不识别 <br>
        $str_1 = fgets($rh);   
        $str_2 = fgets($rh);
        //换行读取再次读取还会继续上次的读取位置继续读取
        echo $str_1;
        echo "<hr>";
        echo $str_2;

        //file 将文件内容转化为数组,<br>直接转化为换行,回车作为分隔符
        $arr = file('PHP_3.txt');
        print_r($arr);

        echo "<hr>";

        //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 "<hr>";
        }

        //读取目录
        print_r(scandir('.'));

        //创建一个新的文件夹
    //  mkdir('asdasd');

        //删除整个文件夹  删除目录必须保证目录内部没有其他文件
    //  $is_bool = rmdir('1');

        //删除 
    //  unlink('PHP_3.txt');

        //获取文件创建时间
        echo filectime('PHP_3.txt');

        echo "<hr>";

        //返回文件最后访问的时间
        echo fileatime('PHP_3.txt');

        echo "<hr>";
        //解析文件具体名称
        echo basename('PHP_3.txt','txt');

        echo "<hr>";
        //获取当前文件所在的目录的名称
        echo dirname('file/PHP_3.txt');

        echo "<hr>";

        //返回全程,拓展名,文件名
        print_r(pathinfo("PHP_3.txt"));

        //修改文件目录权限


        echo "<hr>";
        fclose($rh);
        fclose($rh_1);
    ?>
  • 3
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值