zoukankan      html  css  js  c++  java
  • H5学习_番外篇_PHP数据库操作

    这里写图片描写叙述

    1. 文件操作

    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");

    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); ?

    >

  • 相关阅读:
    <html>
    Android逆向之旅---带你爆破一款应用的签名验证问题
    Android Studio关于USB device not found的解决的方法
    在eclipse中使用github进行代码的上传操作以及如何建立分支
    eclipse 代码上传github 笔记
    如何从Eclipse导入github上的项目源码
    SourceTree的基本使用
    gradle配置国内镜像
    Gradle修改Maven仓库地址
    Gradle仓库配置
  • 原文地址:https://www.cnblogs.com/liguangsunls/p/7137771.html
Copyright © 2011-2022 走看看