首页 > 编程语言 >php 文件读取和写入详细介绍例子

php 文件读取和写入详细介绍例子

时间:2023-04-23 16:35:07浏览次数:39  
标签:echo fopen 读取 写入 filename myfile fh txt php

*************介绍PHP文件的写入 和 读取**************
/**
*文件写入
*/
//覆盖写入
 
$filename = 'leyangjun.txt';
 $word = "北京欢迎你!";
 $fh = fopen($filename, "w");
 echo fwrite($fh, $word); 
 fclose($fh);

//追加写入
 $filename = 'leyangjun.txt';
 $word = "你好!";
 $fh = fopen($filename, "a"); //参数选择 a ,则表示在文件后面追加写入: 
 echo fwrite($fh, $word);
 fclose($fh);

//换行写入
 $filename = 'leyangjun.txt';
 $word = "北京欢迎你!\r\n"; // \r\n换行
 $fh = fopen($filename, "a");
 echo fwrite($fh, $word);
 fclose($fh);

//写封装
 $filename = 'leyangjun.txt';
 $word = "北京欢迎你!\r\n";
 // 确定文件存在并且可写
 if (is_writable($filename)) {
     //打开文件
     if (!$fh = fopen($filename, 'a')) {
          echo "不能打开文件 $filename";
          exit;
     }
     // 写入内容
     if (fwrite($fh, $word) === FALSE) {
         echo "不能写入到文件 $filename";
         exit;
 
    }
    $fh = fopen($filename, "a"); //参数选择 a ,则表示在文件后面追加写入:
     fwrite($fh, $word); 
    echo "成功地将 $word 写入到文件 $filename";
     fclose($fh);
 } else {
     echo "文件 $filename 不可写";
 }


/** 
 *文件读取
 
 */
//读取文件内容,字符串输出
 $myfile = fopen("leyangjun.txt", "r") or die("Unable to open file!");
 echo fread($myfile,filesize("leyangjun.txt"));
 fclose($myfile);

//fgets()函数用于从文件读取单行。
 $myfile = fopen("leyangjun.txt", "r") or die("Unable to open file!");
 echo fgets($myfile);
 fclose($myfile);

//推荐--->逐行读取(feof()函数检查是否已到达 "end-of-file",对于遍历未知长度的数据很有用)
 $myfile = fopen("leyangjun.txt", "r") or die("Unable to open file!");
 // 输出单行直到 end-of-file
 while(!feof($myfile)) {
   echo fgets($myfile) . "<br>";
 }
 fclose($myfile);

//fgetc()函数用于从文件中读取单个字符。
 $myfile = fopen("leyangjun.txt", "r") or die("Unable to open file!");
 // 输出单字符直到 end-of-file
 while(!feof($myfile)) {
   echo fgetc($myfile);
 }
 fclose($myfile);

//你可以把你拿到的字符存储到你的逻辑中处理
 $fp = fopen("leyangjun.txt", "r");
 $arrData = array();
 while(! feof($fp)){    
     $arrData[] = fgets($fp);
 }
 echo '<pre>';print_r($arrData);exit;
 fclose($fp);

标签:echo,fopen,读取,写入,filename,myfile,fh,txt,php
From: https://blog.51cto.com/u_16085147/6218181

相关文章

  • php 无限极分类 封装
    <?phpnamespaceApp\Services;useIlluminate\Http\Request;/***ClassPendingService*@packageApp\Service*无限分类公共类*/classLimitlessService{protected$_request;//publicfunction__construct(Request$request)//{//......
  • 关于【安全狗】在【phpstudy】中【无法找到apache服务名】的问题
      网上很多说就是在安装安全狗apache版的时候,安装程序找不到apache的服务名。   然后看了网上很多教程说就是把phpstudy的允许模式改为【系统服务】模式就行  但是我改了之后在服务里面还是没有找到apache的服务。   这里我记录下,给那些有需要的小伙伴   我......
  • java:文件写入BufferedOutputStream写入字节和PrintWriter写入字符
    BufferedOutputStream和FileOutputStream写入二进制字节方法定义publicBufferedOutputStream(OutputStreamout){示例BufferedOutputStreamwriter=newBufferedOutputStream(newFileOutputStream("demo.txt"));writer.write("helloworld".getBytes());w......
  • Mysql中如果建立了索引,索引所占的空间随着数据量增长而变大,这样无论写入还是查询,性能
    索引所占空间的增长确实会对MySQL数据库的写入性能和查询性能造成影响,这主要是由于索引数据过多时会导致磁盘I/O操作变得非常频繁,从而使性能下降。为此,可以采取以下几种方式来减缓这种影响: 1.限制索引的大小:可以考虑为索引指定大小限制,在存储时仅存储指定大小内的数据。例如,在......
  • JavaTPoint PHP 中文教程【翻译完成】
    在线阅读在线阅读(Gitee)ApacheCN学习资源目录PHP教程Laravel教程WordPress教程CodeIgniter教程Magento2教程Joomla教程Phalcon教程YII框架XAMPP教程贡献指南本项目需要校对,欢迎大家提交PullRequest。请您勇敢地去翻译和改进翻译。虽然我们追求卓越,但我们并不要求您做到......
  • flutter入门实战——文件读取和写入
    问题背景本文将介绍flutter中如何读取文件内容以及保存内容到文件。问题分析先直接上效果:问题解决话不多说,直接上代码。main.dart文件,代码如下:import'dart:async';import'dart:io';import'package:flutter/material.dart';import'package:path_provider/path_provid......
  • Vulhub 漏洞学习之:ThinkPHP
    Vulhub漏洞学习之:ThinkPHP目录Vulhub漏洞学习之:ThinkPHP0利用工具1ThinkPHP2.x任意代码执行漏洞1.1环境安装1.2漏洞利用过程1.3GetShell2ThinkPHP3.x2.1ThinkPHP3.x日志泄露漏洞2.1.1漏洞原理2.1.2漏洞利用过程3ThinkPHP55.0.20远程代码执行漏洞3.1环境安装3.......
  • centos7编译安装php8.1
    一下载源码包 wgethttps://www.php.net/distributions/php-8.1.1.tar.gz 二解压 三安装依赖软件yuminstall-yoniguruma-developenssl-develgccgcc-c++wgetmakelibxml2libxml2-developenssl\openssl-develcurlcurl-devellibjpeglibjpeg-devellibpn......
  • Bootstrap Table表格中存放下拉框,读取数据填充到下拉框
    初学Bootstarp,个人感觉一个功能非常强大的前端框架,由于学习的路途也不是一番风顺的,难免会遇到问题,将遇到的问题整理,可以时常看看,加深记忆。最近做表格需要在表格中加入<select>标签,将数据填充到下拉框,无奈搞了好几个小时也没有成功,最后经过请教身边的大佬,得以解决。代码如下:htt......
  • PHP7革新与性能优化
    有幸参与2015年的PHP技术峰会(PHPCON),听了鸟哥(惠新宸)的关于PHP7的新特性和性能优化的分享,一切都令人感到激动。鸟哥是国内最权威的PHP专家,他的分享有很多非常有价值的东西,我通过整理分享的PPT和收集相关资料,整理为这篇解读性质的技术文章,希望能给做PHP开发的同学一些帮助。 PHP已......