php学习笔记--高级教程--读取文件、创建文件、写入文件
打开文件:fopen:fopen(filename,mode);//fopen("test.txt","r”);
打开模式:r 只读方式打开,将文件指针指向文件头
r+ 读写方式打开,将文件指针指向文件头
w 写入方式,指向文件头,如果不存在则尝试创建
w+ 读写方式,指向文件头,如果不存在则尝试创建
a 写入方式打开,指向文件末尾,如果不存在则尝试创建
a+ 读写方式打开,指向文件末尾,如果不存在则尝试创建
读取文件:fread:fread();
readfile(filename):读取文件内容,并把它写入输出缓冲
<?php echo readfile("webdictionary.txt"); ?>
<?php $myfile = fopen("webdictionary.txt", "r") or die("Unable to open file!"); echo fread($myfile,filesize("webdictionary.txt")); fclose($myfile); ?>
fopen也用于创建文件,如果打开的不存在的文件,此函数会创建文件,嘉定文件被打开为写入(w)或者增加(a)。
模式 | 描述 |
---|---|
r | 打开文件为只读。文件指针在文件的开头开始。 |
w | 打开文件为只写。删除文件的内容或创建一个新的文件,如果它不存在。文件指针在文件的开头开始。 |
a | 打开文件为只写。文件中的现有数据会被保留。文件指针在文件结尾开始。创建新的文件,如果文件不存在。 |
x | 创建新文件为只写。返回 FALSE 和错误,如果文件已存在。 |
r+ | 打开文件为读/写、文件指针在文件开头开始。 |
w+ | 打开文件为读/写。删除文件内容或创建新文件,如果它不存在。文件指针在文件开头开始。 |
a+ | 打开文件为读/写。文件中已有的数据会被保留。文件指针在文件结尾开始。创建新文件,如果它不存在。 |
x+ | 创建新文件为读/写。返回 FALSE 和错误,如果文件已存在。 |
<?php $myfile = fopen("newfile.txt", "w") or die("Unable to open file!"); $txt = "Bill Gates\n"; fwrite($myfile, $txt); $txt = "Steve Jobs\n"; fwrite($myfile, $txt); fclose($myfile); ?>
fread($myfile,filesize("webdictionary.txt"));
fget(资源,长度) //获取文件内容,,若长度为10,则可以获得9位 fgets():用来读取单行,fgets(file,lenght),lenght可选,规定要读取的字节数。默认是1024字节。
从 file 指向的文件中读取一行并返回长度最多为 length - 1 字节的字符串。碰到换行符(包括在返回值中)、EOF 或者已经读取了length - 1 字节后停止(要看先碰到那一种情况)。如果没有指定 length,则默认为 1K,或者说 1024 字节。
若失败,则返回 false。
<?php $myfile = fopen("webdictionary.txt", "r") or die("Unable to open file!"); echo fgets($myfile); fclose($myfile); ?>
fclose(资源) //关闭一个打开的文件资源 unlink(文件) //删除一个文件
feof():检查是否已经到达结尾。对于遍历未知长度的数据很有用。
<?php $myfile = fopen("webdictionary.txt", "r") or die("Unable to open file!"); // 输出单行直到 end-of-file while(!feof($myfile)) { echo fgets($myfile) . "<br/>"; } fclose($myfile); ?>
filesize(filename):filename为文件名,是字符串类型。本函数的结果会被缓存,清试用clearstatcache()来清除缓存。 filetype();

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

AI Hentai Generator
Generate AI Hentai for free.

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



How to solve PHPWarning:fopen():SSLoperationfailedinfile.phponlineX In PHP programming, we often use the fopen function to open files or URLs and perform related operations. However, when using the fopen function, sometimes you will encounter something similar to Warning:fopen():SSLoperationfailedinfile.p

How to solve PHPWarning:fopen():failedtoopenstream:Nosuchfileordirectory In the process of using PHP development, we often encounter some file operation problems, one of which is "PHPWarning:fopen():failedtoopenstream:Nosuchfileordirectory"

The garbled code in php+fread() is because the encoding of the output page is inconsistent with the encoding of the read file. The solution: 1. Open the corresponding PHP file; 2. Read the file through the fread function; 3. Through "iconv('gbk' , 'utf-8', $data)" method to transcode the read content.

How to solve PHPWarning:fopen():failedtoopenstream:Permissiondenied In the process of developing PHP programs, we often encounter some error messages, such as PHPWarning:fopen():failedtoopenstream:Permissiondenied. This error is usually due to incorrect file or directory permissions

In Matlab, the fopen function is used to open a file and return the file identifier for subsequent reading or writing operations on the file. Select the appropriate permission options to open the file as needed, and promptly close the file when the operation is complete. It should be noted that after opening a file, you need to ensure that the file is closed in time when it is no longer needed to release system resources. In addition, if the file opening fails or an operation error occurs, the error handling mechanism can be used to handle it accordingly.

Use Java's String.length() function to get the length of a string. In Java programming, string is a very common data type. We often need to get the length of a string, that is, the number of characters in the string. In Java, we can use the length() function of the String class to get the length of a string. Here is a simple example code: publicclassStringLengthExample{publ

The fread() function reads data from an open file. The fread() function stops at the end of the file or when it reaches the specified length. Returns the read string on success. Returns FALSE on failure. Syntax fread(file_pointer,length) Parameter file_pointer−The file system pointer resource created using fopen(). Required. length−Maximum number of bytes to read. Required. Return Value If successful, the fread() function returns the read string. On failure, returns FALSE. Suppose we have a file called "one.txt" where

In PHP development, file operations are very common. Under normal circumstances, we need to perform file reading, writing, deletion and other operations. Among them, the fopen function and fread function can be used to read the file, and the fopen function, fwrite function and fclose function can be used to write the file. This article will introduce how PHP uses fopen, fwrite and fclose to perform file operations. 1. fopen function The fopen function is used to open files. Its syntax is as follows: r
