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.

  • 相关阅读:
    Python从文件中读取数据
    Python中类的继承
    Python中的类(2)
    Python中的类
    自动登陆抽屉(1)
    爬取汽车之家新闻
    Django简介
    web应用,http协议简介,web框架
    CentOS7安装python3.6
    MySQL之索引
  • 原文地址:https://www.cnblogs.com/cyany/p/10011702.html
Copyright © 2011-2022 走看看