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.

  • 相关阅读:
    P1041 传染病控制(dfs)
    洛谷P1040 加分二叉树(树形dp)
    微信支付移动开发
    UVA
    Android开源框架ViewPageIndicator和ViewPager实现Tab导航
    Android Application Digital Signatures
    android:怎样在TextView实现图文混排
    python无私有成员变量
    unity3D游戏开发实战原创视频讲座系列11之相扑游戏开发并公布到WinWP8
    MFC:Win32-Dll及MFC-Dll编写调用
  • 原文地址:https://www.cnblogs.com/cyany/p/10011702.html
Copyright © 2011-2022 走看看