zoukankan      html  css  js  c++  java
  • php:文件操作

    <?php
    //目录操作
    mkdir("./aa"); //创建目录
    rmdir("./test"); //删除目录,只能删除空文件夹
    rename("./test","./aa/test");//重命名、移动文件夹

    //文件操作
    touch("./aa.txt");//创建文件
    copy("./aa.txt","../aa.txt"); //复制文件
    unlink("./aa.txt");//删除文件
    echo file_get_contents("./aa.txt"); //读取文件内容
    echo file_get_contents("http://www.baidu.com"); 读取远程文件

    file_put_contents("./aa.txt",file_get_contents("http://www.baidu.com"));//向文件里面写入内容

    readfile("./aa.txt");//读取文件内容并显示
    var_dump(file("./aa.txt")); //读取文件内容,并且返回数组,数组里面存的是每一行

    //文件内容操作
    //打开文件资源

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

    'r' 只读方式打开,将文件指针指向文件头。
    'r+' 读写方式打开,将文件指针指向文件头。
    'w' 写入方式打开,将文件指针指向文件头并将文件大小截为零。如果文件不存在则尝试创建之。
    'w+' 读写方式打开,将文件指针指向文件头并将文件大小截为零。如果文件不存在则尝试创建之。
    'a' 写入方式打开,将文件指针指向文件末尾。如果文件不存在则尝试创建之。
    'a+' 读写方式打开,将文件指针指向文件末尾。如果文件不存在则尝试创建之。
    'x' 创建并以写入方式打开,将文件指针指向文件头。如果文件已存在,则 fopen() 调用失败并返回 FALSE ,并生成一条 E_WARNING 级别的错误信息。如果文件不存在则尝试创建之。这和给 底层的 open(2) 系统调用指定 O_EXCL|O_CREAT 标记是等价的。
    'x+' 创建并以读写方式打开,其他的行为和 'x' 一样。
    'c' Open the file for writing only. If the file does not exist, it is created. If it exists, it is neither truncated (as opposed to 'w'), nor the call to this function fails (as is the case with 'x'). The file pointer is positioned on the beginning of the file. This may be useful if it's desired to get an advisory lock (see flock() ) before attempting to modify the file, as using 'w' could truncate the file before the lock was obtained (if truncation is desired, ftruncate() can be used after the lock is requested).
    'c+' Open the file for reading and writing; otherwise it has the same behavior as 'c'


    $fp = fopen("./aa.txt","a");//两个参数,第一个参数是开启的文件,第二个参数是用哪种方式开启,有打开就有关闭,一定记住关闭文件资源

    fwrite($fp,"aaaaaa"); //写入内容,w方式会覆盖,a方式会追加

    while($v = fgetc($fp)) //读取文件内容,一个一个字符读取
    {
    echo $v;
    }
    echo fgets($fp); //获取文件内容,一行一行获取
    echo fread($fp,2); //读取文件内容,可以读取特定长度的内容

    //关闭文件资源
    fclose($fp);

    $dir = opendir("./aa");

    while($v = readdir($dir))
    {
    echo $v."<br>";
    }

    closedir($dir);


    ?>

  • 相关阅读:
    Java基础知识强化97:final、finally、finally区别
    Java基础知识强化之集合框架笔记02:集合的继承体系图解
    Java基础知识强化之集合框架笔记01:集合的由来与数组的区别
    Java基础知识强化96:Calendar类之获取任意年份的2月有多少天的案例
    Java基础知识强化95:Calendar类之Calendar类的add()和set()方法
    Gym
    Gym
    Good Bye 2015 B. New Year and Old Property —— dfs 数学
    HDU1873 看病要排队 —— 优先队列(STL)
    HDU5877 Weak Pair dfs + 线段树/树状数组 + 离散化
  • 原文地址:https://www.cnblogs.com/nannan-0305/p/5528552.html
Copyright © 2011-2022 走看看