• php file opearate


    1. r

    open the file and read only

    2.r+
    open the file and write

    3.w
    open the file and write only truncates the file to zero length.

    4.w+
    open the file and write truncates the file to zero length.

    5.a
    open the file and write ,not clip the data ,add data to end

    6.a+
    open the file and read ,write no clip the data, add data to end


    fopen() if open fail then return false
    fclose() if close fail then return false

    open the file need fou step

    1. $file = fopen(filename,mode)
      2.compute filesize $filesize = filesize($file);
      3.$filedata =fread($file,$filesize);
      4.fclose($file);
    <?php
    
    /**
     * @Author: cyany_blue
     * @Date:   2018-11-24 12:16:32
     * @Last Modified by:   cyany_blue
     * @Last Modified time: 2018-11-24 12:21:36
     */
    $filename = 'data.txt';
    $file = fopen($filename, 'r');
    if($file ==false){
    	echo "open file failed";
    	exit();
    }else{
    	$filesize = filesize($filename);
    	$filetext = fread($file,$filesize);
    	fclose($file);
    	echo "file size:".$filesize;
    	echo "<pre>$filetext</pre>";
    }
    

    fwrite

    $resouce = 'data2.txt';
    $file2 = fopen($resouce, 'w+');
    $fileres = fwrite($file2, 'i am newly add it');
    fclose($file2);
    // sleep(2);
    $newFile = fopen($resouce, 'r');
    $newFileSize = filesize($resouce);
    $content = fread($newFile, $newFileSize);
    echo $content;
    fclose($newFile);
    

    Summry:
    fopen need two arguments,one is filename,other is mode
    fread need two arguments,one is file resource(return by fopen),other is filesize,we can use filesize($filename) to get filesize.
    fwrite need two arguments ,one is file resource,other is data needed to add
    fclose need one argument ,is file resource.


    ok ,that is all .thanks.

  • 相关阅读:
    BZOJ 3910 火车 倍增LCA
    CF1012B Chemical table 构造_思维_并查集
    CF 949C Data Center Maintenance_强联通分量_思维题
    CF949B A Leapfrog in the Array 思维题,推理
    关于前端的思考与感悟
    打造专属自己的html5拼图小游戏
    好看的轮播切换效果
    the compatibility problem of ie
    SVG Sprite 入门(SVG图标解决方案)
    Top 15
  • 原文地址:https://www.cnblogs.com/cyany/p/10011702.html
Copyright © 2020-2023  润新知