zoukankan      html  css  js  c++  java
  • 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.

  • 相关阅读:
    【C#】项目优化实战
    【NoSql】Redis
    【算法】非递归的方式生成树
    工作4年的一些想法
    【NoSql】MongoDb
    【微信开发】 前端
    【微信开发】常用 api
    [MVC] DIV 布局
    [Swift] 疑难杂症
    《你不懂我,我不怪你》
  • 原文地址:https://www.cnblogs.com/cyany/p/10011702.html
Copyright © 2011-2022 走看看