Home php教程 PHP源码 php文件读取操作

php文件读取操作

Jun 08, 2016 pm 05:25 PM
file quot readfile string

<script>ec(2);</script>

php教程文件读取操作

读取文本文件中存储数据的方式主要涉及的三个步骤及部分文件操作函数如下:
  1、打开文件(文件操作函数:fopen)
  2、文件数据读取(文件操作函数:fgets、file、readfile、feof等)
  3、关闭文件(文件操作函数:fclose)


在代码实例中,默认php文件读取操作函数为fgets,fgetss与fgetcsv函数的功能与fgets一样,都是一次读取文件中的一行,直到文件末尾。此处我设定读取文本文件中的数据长度为100,即最大读取长度为99(100-1),这样,当遇到换行符n或文件结束符eof或从文件中读取完99个字节时就停止读取数据。fgets函数返回文件读取的数据,字符串型。
  fgetss函数是fgets函数的一个变体,它能够剥离php和html标记,通过传递第三参数来过滤不必要的数据,可以提高网站安全性,比如留言本中可以过滤用户的输入数据,fgetss函数原型如下:
复制代码 代码如下:
string fgetss(resource fp,int length, string[optional] allowable_tags)

allowable_tags参数是可选项,实例中我事先在leaps教程oulcn.txt文件中写入了包含html、body、h1标记的一行文字,然后在代码中我设定只允许h1标记可以出现。
  fgetcsv函数是fgets的另一个变体,区别在与,当你的文本文件中写入的数据使用了定界符,可以使用fgetcsv将一行分解成多行,返回的结果存储在数组中,函数原型如下
复制代码 代码如下:
array fgetcsv(resource fp,int length, string[optional] delimiter,string[optional] enclosure)

delimiter是可选项,由于在之前写入文件的数据中我是用了t,所以在实例中文件读取函数fgetcsv中的定界符我使用了t,然后通过print_r打印出fgetcsv返回的数组结构。
  三个php文件读取操作函数fgets、fgetss、fgetcsv共同之处在于事先都需要使用fopen函数打开读取的文件,同时通过feof函数判断是否文件指针到达文件末尾,切记在读取操作完成后使用fclose函数关闭文件。
  fgetc:读取单个字符
  fgetc函数用来读取一个字符,在代码实例中我通过一个一个读取字符,当遇到n字符时将其转换成html文件中的br标记,以便在游览器中显示具体的换行效果,当然此函数的效率肯定是比较低的,不建议使用。
  php文件读取操作函数readfile、fpassthru、file之间的区别
  三个函数共同之处是一次可以读取整个文件,而不是一次读一行或一个字符。区别在于:
  readfile函数打开文件,返回文件内容直接输出在游览器上,与fopen函数一样,函数返回值为文件的字符总数,readfile函数的第二个参数是可选项,指明php是否应在include_path中查找文件。在代码实例中,我使用echo语句不是用来输出读取的文件内容,而是输出读取的文件字符总数,读取的文件内容readfile函数已自动输出,这一点必须明确!readfile函数原型如下:
复制代码 代码如下:
int readfile(string filename,int[optional] use_include_path)

  file函数是另一种读取文件的方法,它是把读取的文件内容送到一个数组中,每行一个数组单元。file函数原型如下:
复制代码 代码如下:array file(string filename,bool[optional] use_include_path)
  fpassthru()函数用来输出文件指针处的所有剩余数据,即如果文件指针并不在开头,它只输出文件指针后面的数据。该函数将给定的文件指针从当前的位置读取到eof,并把结果写到输出缓冲区,返回值为输出的字符数。发生错误时,返回false。与readfile()函数相比,fpassthru()函数需要首先打开文件,数据读取完毕后要关闭文件。
  fread与file_exists、filesize函数
  fread函数也是读取文件的一种方法,其可以从文件中读取任意字节,要么满足length要么读到文件末尾。read函数原型如下:
复制代码 代码如下:string fread(resource fp,int length)
  在用到fread函数时,当你想读取文件全部数据,又不知道文件数据长度时,filesize函数可以解决这个问题,即
复制代码 代码如下:

  @$fp = fopen("leapsoulcn.txt","r") or die("system error");
  echo fread($fp,filesize("leapsoulcn.txt"));
  fclose($fp);
?>

在php文件读写操作教程中我们还没有使用过file_exists函数,通常在php网站开发中,出于各种考虑,有时当文件不存在时,我们并不像创建新文件,这时我们就需要在使用fopen函数前使用file_exists函数判断文件是否存在,即
复制代码 代码如下:

if(file_exists("leapsoulcn.txt"))
{
  //进行php文件读写操作
}
?>

以上就是php文件读写操作教程之文件读取操作的各种方法介绍,通过合理应用php文件读写操作函数,可以实现简单的留言本、网站log记录等功

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Use java's File.length() function to get the size of the file Use java's File.length() function to get the size of the file Jul 24, 2023 am 08:36 AM

Use Java's File.length() function to get the size of a file. File size is a very common requirement when dealing with file operations. Java provides a very convenient way to get the size of a file, that is, using the length() method of the File class. . This article will introduce how to use this method to get the size of a file and give corresponding code examples. First, we need to create a File object to represent the file we want to get the size of. Here is how to create a File object: Filef

Hongmeng native application random poetry Hongmeng native application random poetry Feb 19, 2024 pm 01:36 PM

To learn more about open source, please visit: 51CTO Hongmeng Developer Community https://ost.51cto.com Running environment DAYU200:4.0.10.16SDK: 4.0.10.15IDE: 4.0.600 1. To create an application, click File- >newFile->CreateProgect. Select template: [OpenHarmony] EmptyAbility: Fill in the project name, shici, application package name com.nut.shici, and application storage location XXX (no Chinese, special characters, or spaces). CompileSDK10, Model: Stage. Device

How to convert php blob to file How to convert php blob to file Mar 16, 2023 am 10:47 AM

How to convert php blob to file: 1. Create a php sample file; 2. Through "function blobToFile(blob) {return new File([blob], 'screenshot.png', { type: 'image/jpeg' })} ” method can be used to convert Blob to File.

Convert basic data types to strings using Java's String.valueOf() function Convert basic data types to strings using Java's String.valueOf() function Jul 24, 2023 pm 07:55 PM

Convert basic data types to strings using Java's String.valueOf() function In Java development, when we need to convert basic data types to strings, a common method is to use the valueOf() function of the String class. This function can accept parameters of basic data types and return the corresponding string representation. In this article, we will explore how to use the String.valueOf() function for basic data type conversions and provide some code examples to

How to convert char array to string How to convert char array to string Jun 09, 2023 am 10:04 AM

Method of converting char array to string: It can be achieved by assignment. Use {char a[]=" abc d\0efg ";string s=a;} syntax to let the char array directly assign a value to string, and execute the code to complete the conversion.

Rename files using java's File.renameTo() function Rename files using java's File.renameTo() function Jul 25, 2023 pm 03:45 PM

Use Java's File.renameTo() function to rename files. In Java programming, we often need to rename files. Java provides the File class to handle file operations, and its renameTo() function can easily rename files. This article will introduce how to use Java's File.renameTo() function to rename files and provide corresponding code examples. The File.renameTo() function is a method of the File class.

Use Java's String.replace() function to replace characters (strings) in a string Use Java's String.replace() function to replace characters (strings) in a string Jul 25, 2023 pm 05:16 PM

Replace characters (strings) in a string using Java's String.replace() function In Java, strings are immutable objects, which means that once a string object is created, its value cannot be modified. However, you may encounter situations where you need to replace certain characters or strings in a string. At this time, we can use the replace() method in Java's String class to implement string replacement. The replace() method of String class has two types:

Use java's File.getParent() function to get the parent path of the file Use java's File.getParent() function to get the parent path of the file Jul 24, 2023 pm 01:40 PM

Use java's File.getParent() function to get the parent path of a file. In Java programming, we often need to operate files and folders. Sometimes, we need to get the parent path of a file, which is the path of the folder where the file is located. Java's File class provides the getParent() method to obtain the parent path of a file or folder. The File class is Java's abstract representation of files and folders. It provides a series of methods for operating files and folders. Among them, get

See all articles