国产av日韩一区二区三区精品,成人性爱视频在线观看,国产,欧美,日韩,一区,www.成色av久久成人,2222eeee成人天堂

Home php教程 PHP源碼 php文件讀取操作

php文件讀取操作

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

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

php教程文件讀取操作

讀取文本文件中存儲數(shù)據(jù)的方式主要涉及的三個步驟及部分文件操作函數(shù)如下:
  1、打開文件(文件操作函數(shù):fopen)
  2、文件數(shù)據(jù)讀取(文件操作函數(shù):fgets、file、readfile、feof等)
  3、關(guān)閉文件(文件操作函數(shù):fclose)


在代碼實例中,默認php文件讀取操作函數(shù)為fgets,fgetss與fgetcsv函數(shù)的功能與fgets一樣,都是一次讀取文件中的一行,直到文件末尾。此處我設(shè)定讀取文本文件中的數(shù)據(jù)長度為100,即最大讀取長度為99(100-1),這樣,當(dāng)遇到換行符n或文件結(jié)束符eof或從文件中讀取完99個字節(jié)時就停止讀取數(shù)據(jù)。fgets函數(shù)返回文件讀取的數(shù)據(jù),字符串型。
  fgetss函數(shù)是fgets函數(shù)的一個變體,它能夠剝離php和html標(biāo)記,通過傳遞第三參數(shù)來過濾不必要的數(shù)據(jù),可以提高網(wǎng)站安全性,比如留言本中可以過濾用戶的輸入數(shù)據(jù),fgetss函數(shù)原型如下:
復(fù)制代碼 代碼如下:
string fgetss(resource fp,int length, string[optional] allowable_tags)

allowable_tags參數(shù)是可選項,實例中我事先在leaps教程oulcn.txt文件中寫入了包含html、body、h1標(biāo)記的一行文字,然后在代碼中我設(shè)定只允許h1標(biāo)記可以出現(xiàn)。
  fgetcsv函數(shù)是fgets的另一個變體,區(qū)別在與,當(dāng)你的文本文件中寫入的數(shù)據(jù)使用了定界符,可以使用fgetcsv將一行分解成多行,返回的結(jié)果存儲在數(shù)組中,函數(shù)原型如下
復(fù)制代碼 代碼如下:
array fgetcsv(resource fp,int length, string[optional] delimiter,string[optional] enclosure)

delimiter是可選項,由于在之前寫入文件的數(shù)據(jù)中我是用了t,所以在實例中文件讀取函數(shù)fgetcsv中的定界符我使用了t,然后通過print_r打印出fgetcsv返回的數(shù)組結(jié)構(gòu)。
  三個php文件讀取操作函數(shù)fgets、fgetss、fgetcsv共同之處在于事先都需要使用fopen函數(shù)打開讀取的文件,同時通過feof函數(shù)判斷是否文件指針到達文件末尾,切記在讀取操作完成后使用fclose函數(shù)關(guān)閉文件。
  fgetc:讀取單個字符
  fgetc函數(shù)用來讀取一個字符,在代碼實例中我通過一個一個讀取字符,當(dāng)遇到n字符時將其轉(zhuǎn)換成html文件中的br標(biāo)記,以便在游覽器中顯示具體的換行效果,當(dāng)然此函數(shù)的效率肯定是比較低的,不建議使用。
  php文件讀取操作函數(shù)readfile、fpassthru、file之間的區(qū)別
  三個函數(shù)共同之處是一次可以讀取整個文件,而不是一次讀一行或一個字符。區(qū)別在于:
  readfile函數(shù)打開文件,返回文件內(nèi)容直接輸出在游覽器上,與fopen函數(shù)一樣,函數(shù)返回值為文件的字符總數(shù),readfile函數(shù)的第二個參數(shù)是可選項,指明php是否應(yīng)在include_path中查找文件。在代碼實例中,我使用echo語句不是用來輸出讀取的文件內(nèi)容,而是輸出讀取的文件字符總數(shù),讀取的文件內(nèi)容readfile函數(shù)已自動輸出,這一點必須明確!readfile函數(shù)原型如下:
復(fù)制代碼 代碼如下:
int readfile(string filename,int[optional] use_include_path)

  file函數(shù)是另一種讀取文件的方法,它是把讀取的文件內(nèi)容送到一個數(shù)組中,每行一個數(shù)組單元。file函數(shù)原型如下:
復(fù)制代碼 代碼如下:array file(string filename,bool[optional] use_include_path)
  fpassthru()函數(shù)用來輸出文件指針處的所有剩余數(shù)據(jù),即如果文件指針并不在開頭,它只輸出文件指針后面的數(shù)據(jù)。該函數(shù)將給定的文件指針從當(dāng)前的位置讀取到eof,并把結(jié)果寫到輸出緩沖區(qū),返回值為輸出的字符數(shù)。發(fā)生錯誤時,返回false。與readfile()函數(shù)相比,fpassthru()函數(shù)需要首先打開文件,數(shù)據(jù)讀取完畢后要關(guān)閉文件。
  fread與file_exists、filesize函數(shù)
  fread函數(shù)也是讀取文件的一種方法,其可以從文件中讀取任意字節(jié),要么滿足length要么讀到文件末尾。read函數(shù)原型如下:
復(fù)制代碼 代碼如下:string fread(resource fp,int length)
  在用到fread函數(shù)時,當(dāng)你想讀取文件全部數(shù)據(jù),又不知道文件數(shù)據(jù)長度時,filesize函數(shù)可以解決這個問題,即
復(fù)制代碼 代碼如下:

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

在php文件讀寫操作教程中我們還沒有使用過file_exists函數(shù),通常在php網(wǎng)站開發(fā)中,出于各種考慮,有時當(dāng)文件不存在時,我們并不像創(chuàng)建新文件,這時我們就需要在使用fopen函數(shù)前使用file_exists函數(shù)判斷文件是否存在,即
復(fù)制代碼 代碼如下:

if(file_exists("leapsoulcn.txt"))
{
  //進行php文件讀寫操作
}
?>

以上就是php文件讀寫操作教程之文件讀取操作的各種方法介紹,通過合理應(yīng)用php文件讀寫操作函數(shù),可以實現(xiàn)簡單的留言本、網(wǎng)站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

Undress AI Tool

Undress AI Tool

Undress images for free

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.

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)

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.

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

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

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.

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.

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

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:

See all articles