• 【二十六】php之文件编程


    1.获取文件的相关信息

    fopen、fstat、fclose(打开文件、获取文件相关信息、关闭文件)

    filesize、filectime、filemtime、fileatime(文件大小、上次change时间、文件修改时间、文件访问时间)

    <?php
    // 方式一:
        $file_path="test.txt";
        // 打开文件
        // a+ 代表读写方式,并将指针取向末尾
        if($fp=fopen($file_path,"a+" )){
            $file_info=fstat($fp);
            echo "<pre>";
            print_r($file_info);    //打印这个文件的相关信息。返回的是一个关联数组
            echo "</pre>";
            echo "<br/>文件大小是{$file_info['size']}";
            echo  "<br/>文件上次修改时间是".date("Y-m-d H:i:s",$file_info['mtime']);
            echo  "<br/>文件上次访问时间是".date("Y-m-d H:i:s",$file_info['atime']);
            echo  "<br/>文件上次changge时间是".date("Y-m-d H:i:s",$file_info['ctime']);
        }else
        {
            echo "打开文件失败";
        }
        // 关闭文件
        fclose($fp);
    
    // 方式二:在不打开文件的情况下获取文件的size和ctime
        echo "<br/>".filesize($file_path);
        echo "<br/>".date("Y-m-d H:i:s",fileatime($file_path));
        echo "<br/>".date("Y-m-d H:i:s",filectime($file_path));
        echo "<br/>".date("Y-m-d H:i:s",filemtime($file_path));
    ?>

    2.读取文件内容

    fread(读文件内容)

    file_get_contens、feof、str_replace(获取文件所有内容、检测是否已到达文件末尾、替换字符串内容)

    <?php
        // 1.打开文件
        // 判断文件是否存在
        // 2.读取内容
        // 3.替换回车
        // 4.关闭文件
    
        $file_path="test.txt";
        // 第一种读取方式****************
        if (file_exists($file_path)) {
            $fp=fopen($file_path, "a+");
            $con=fread($fp, filesize($file_path));
            // 替换换行
            $con=str_replace("
    ", "<br/>", $con);
            echo $con;
        }else{
            echo "文件不存在";
        }
    
        // 第二种读取方式****************
        // file_get_contents:读取文件中所有的内容
        $con=file_get_contents($file_path);
        $con=str_replace("
    ", "<br/>", $con);
        echo $con;
    
         // 第三种读取方式****************
         $buff=1024;
         if (file_exists($file_path)) {
             $fp=fopen($file_path, "a+");
             // feof:检测是否已到达文件末尾
             while (!feof($fp)) {
                 $str=fread($fp, $buff);
             }
             $str=str_replace("
    ", "<br/>", $str);
             echo $str;
         }else{
             echo "文件不存在";
         }
    ?>

    3.写文件操作

    fwite(往文件写内容)

    file_put_contents(将一个字符串写入文件

    <?php
    // 1.打开文件
    // 判断文件目录是否存在
    // 2.如果存在,打开目录
    // 3.使用fwrite往文件写内容
    // 4.关闭文件
        echo "方式一:*******************************************************";
        $file_path="test.txt";
        if (file_exists($file_path)) {
            $fp=fopen($file_path, "a+");
            $con="
    fighting~~~haha";
            for ($i=0; $i <10 ; $i++) { 
                fwrite($fp,$con );
            }
        }else{
            echo "文件不存在";
        }
        echo "添加成功";
        fclose($fp);
        echo "方式二:*******************************************************";
        //直接获取文件内容
        //file_put_contents:需要填最后面的参数。如果不填为重写文件内容,而不是追加
        $con2="hello";
        file_put_contents($file_path, $con2,FILE_APPEND);
    ?>

    4.拷贝文件

    copy(拷贝文件

    <?php
        $file="54b5cd917e0747547.png";
        $newfile="test.png";
        if (!copy($file, $newfile)) {
            echo "拷贝失败";
        }else{
            echo "拷贝成功";
        }
    ?>

     5.创建文件夹

    mkdir(新建目录

    <?php
        //创建文件夹
        //这是创建单个文件夹******************************************
        $path="D:/wamp64/www/filestream/cmftest";
        // 1.先判断这个目录是否存在
        // 如果存在则提示该目录已经存在
        // 如果不存在则新建该目录
        if (!is_dir($path)) {
            if (mkdir($path)) {
                echo "创建成功";
            }else{
                echo "创建失败";
            }
        }else{
            echo "该文件夹已经存在了";
        }
        //这是创建多个文件夹(递归)******************************************
        //注意:创建多个需要带参数
        $path="D:/wamp64/www/filestream/cmftest/aaa/bbb/ccc/ddd/eee";
        if (!is_dir($path)) {
            if (mkdir($path,0777,true)) {
                echo "创建成功";
            }else{
                echo "创建失败";
            }
        }else{
            echo "该文件夹已经存在了";
        }
    ?>

    6.删除文件夹

    mkdir(删除文件夹)

    <?php
    //注意:
    //1.不能多级删除
    //2.如果文件夹下面有东西,不能删除成功
        $path="D:/wamp64/www/filestream/cmftest/aaa/bbb/ccc/ddd/eee";
        if (rmdir($path)) {
            echo "删除成功";
        }
    ?>

    7.创建文件

    <?php
        $path="D:/wamp64/www/filestream/cmftest/aa.txt";
        $fp=fopen($path,"w+");
        fwrite($fp, "hello");
        fclose($fp);
        echo "创建文件成功";
    ?>

    8.删除文件

    delete、unlink

    <?php
        $file_path="D:/wamp64/www/filestream/cmftest/aa.txt";
        if (is_file($file_path)) {
            if (unlink($file_path)) {
                echo "删除成功";
            }else{
                echo "删除失败";
            }
        }else{
            echo "这个文件不存在";
        }
    ?>

     上传文件程序:

    updown.php

    <!DOCTYPE html>
    <html>
    <head>
        <title></title>
    </head>
    <body>
    <h1>文件上传</h1>
    <form action="updownpro.php" method="post" enctype="multipart/form-data">
        <table>
        <tr><td><br/>请输入用户名:</td><td><input type="text" name="username"/></td></tr>
        <tr><td><br/>请介绍你上传的文件:</td><td><textarea name="fileintro" rows="2" cols="80"></textarea></td></tr>
        <tr><td><br/>请选择你要上传的文件:</td><td><input type="file" name="myfile"/></td></tr>
        <tr><td><br/><input type="submit" value="上传文件"></td></tr>
            </table>
    </form>
    </body>
    </html>

    updownpro.php

    <?php
        $username=$_POST['username'];
        $fileintro=$_POST['fileintro'];
        // echo "<pre>";
        // print_r($_FILES);
        // print_r($_SERVER);
        // echo "</pre>";
        // 1.先判断文件是否是通过http post上传的
        // 2.如果是,则将文件转移到服务器放上传资源的地方
        //获取文件大小。如果文件超过2m,不允许上传,并退出程序(解决限制文件大小)
        $filesize=$_FILES['myfile']['size'];
        // $filesize的文字大小是以字节数算的
        if ($filesize>2*1024*1024) {
            echo "文件大小超过2m啦";
            exit();
        }
        // 获取文件类型,限制类型上传(解决限制类型)
        // 使用运算符&&。如果两种类型都不是,则提示他类型错误
        if ($_FILES['myfile']['type']!="audio/mp3" && $_FILES['myfile']['type']!="image/png") {
            echo "只能上传图片文件及音乐文件";
            exit();
        }
        // 判断是否上传ok
        if (is_uploaded_file($_FILES['myfile']['tmp_name'])) {
            $uploadfile=$_FILES['myfile']['tmp_name'];
            // 给用户动态生成文件夹(解决多个用户上传同一个文件名的文件问题)
            $userpath=$_SERVER['DOCUMENT_ROOT']."/filestream/cmftest/".$username;
            if (!file_exists($userpath)) {
                mkdir($userpath);
            }
            // 这个路径把所有资源都放在服务器上
            // $movetofile=$_SERVER['DOCUMENT_ROOT']."/filestream/cmftest/".$_FILES['myfile']['name'];
            // 这个路径是把资源放在各自用户的文件夹上
             // $movetofile=$userpath."/".$_FILES['myfile']['name'];
             // 这个路径是为了防止同个用户上传同一个文件名的问题
             $movetofile=$userpath."/".time().rand(1,1000).strrchr($_FILES['myfile']['name'], ".");
                echo "<pre>";
            print_r($_FILES);
            echo "</pre>";
            // iconv:给文件类型转码
            if(move_uploaded_file($uploadfile, iconv("utf-8", "gb2312", $movetofile))){
                echo $_FILES['myfile']['name']."添加成功";
            }else{
                echo "添加失败111111";
            }
        }else{
                echo "添加失败";
            }
    ?>

    如图:

  • 相关阅读:
    linux dns子域授权 split分离解析 缓存dns服务器
    linux kvm虚拟机快速构建及磁盘类型
    linux虚拟化概述
    一个http请求从用户输入网址开始到结束都发生了什么
    Django lazy load 懒加载 倒序查询
    fun = [lambda x: x*i for i in range(4)] 本质解析/原理,LEGB规则 闭包原理
    linux 下mysql服务的管理
    MySQL 增删改查
    redis的应用场景 为什么用redis
    redis中的hash、列表、集合操作
  • 原文地址:https://www.cnblogs.com/8013-cmf/p/8125822.html
Copyright © 2020-2023  润新知