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.

  • 相关阅读:
    LINUX中常用操作命令
    NET Core Kestrel部署HTTPS
    Java中Enum枚举的使用
    Java多线程——sychronized
    Java中SynchronizedMap与ConcurrentHashMap的对比
    Java中HashMap实现原理
    数据结构——Java实现单向链表
    数据库——事务基础
    数据结构——Java实现二叉树
    Java中ThreadLocal的深入理解
  • 原文地址:https://www.cnblogs.com/cyany/p/10011702.html
Copyright © 2011-2022 走看看