php读取文件的方法
PHP读写文件,就如同ASP中使用FSO进行文件的读写操作。当然在ASP中FSO仅对于运行当前程序的服务器磁盘上文件进行读写(很明显就是需要获得物理路径),然而PHP可以通过FTP或HTTP打开文件进行读写。 一,PHP如何读取文件 PHP读取文件可以读取当前服务器或远程服务器中的文件。其步骤是:打开文件、读文件和关闭文件。 1,PHP如何打开文件 使用PHP函数fopen()打开一个文件,fopen()一般使用2个参数表示打开文件的路径和文件模式。比如: |
||||||
其中 "../cnbruce.txt" 就表示打开的cnbruce.txt文件的路径(相对当前执行程序文件的路径),'w'表示以只写的方式打开该文本文件。 附录:fopen()函数的文件模式总结 r 只读——读模式,打开文件,从文件头开始读 r+ 可读可写方式打开文件,从文件头开始读写 w 只写——写方式打开文件,同时把该文件内容清空,把文件指针指向文件开始处。如果该文件已经存在,将删除文件已有内容;如果该文件不存在,则建立该文件 w+ 可读可写方式打开文件,同时把该文件内容清空,把文件指针指向文件开始处。如果该文件不存在,则建立该文件 a 追加 以只写方式打开文件,把文件指针指向文件末尾处。如果该文件不存在,则建立该文件 a+ 追加 以可读可写方式打开文件,把文件指针指向文件末尾处。如果该文件不存在,则建立该文件 b 二进制 用于于其他模式进行连接。建议使用该选项,以获得更大程度的可移植性 注意,如果fopen()函数调用失败,函数将返回false。否则返回指针数据。所以一般在打开了文件后\读写文件前需要检测下文件是否存在。
其中@符号表示PHP将抑制所有由当前函数调用产生的错误。 2,PHP如何读文件 在PHP打开了文件之后就需要对文件进行读取,一般是使用fgets()函数。 该函数可以从文件中每次读取一行内容,其不断读入数据,值到遇到本行的换行符,或者全文的结束符号EOF。 介于fgets()函数只能读取一行数据,所以若需要读取文件的所有数据,须使用循环语句来完成。比如:
其中feof()函数是用来检测文件是否结束的。该函数唯一参数就是文件指针(即$fp对应打开的文件)。 当然,在PHP中还可以使用readfile()函数一次读取整个文件。该函数包括了打开文件、读取文件并输出到浏览器中和关闭文件。比如:
3,PHP如何关闭文件 使用函数fclose()就可以将文件关闭。 二,PHP如何写数据到文件 与PHP读取文件一样,PHP写入文件也需要:打开文件、写入数据和关闭文件。打开和关闭文件的方法上面已经说明,那PHP中写入数据到文件是怎样的呢。 使用fwrite()函数,比如fwrite(文件路径,写入内容):
了解了PHP的读写文件,就可以把最简单的数据存入文本保存了。也就可以做个故事接龙了。 ============================= 其他有用的文件函数: file_exists():查看文件是否存在,返回布尔值 filesize():查看文件大小,可直接echo输出 unlink():删除文件,注意PHP中没有delete函数。 |

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

If you are an experienced PHP developer, you might have the feeling that you’ve been there and done that already.You have developed a significant number of applications, debugged millions of lines of code, and tweaked a bunch of scripts to achieve op

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.
