Home php教程 php手册 php学习笔记--高级教程--读取文件、创建文件、写入文件

php学习笔记--高级教程--读取文件、创建文件、写入文件

Jun 21, 2016 am 08:47 AM
fopen fread length

打开文件:fopen:fopen(filename,mode);//fopen("test.txt","r”); 打开模式:r 只读方式打开,将文件指针指向文件头 r+ 读写方式打开,将文件指针指向文件头 w 写入方式,指向文件头,如果不存在则尝试创建 w+ 读写方式,指向文件头,如果不存在则尝试创建 a 写入方式打开,指向文件末尾,如果不存在则尝试创建 a+ 读写方式打开,指向文件末尾,如果不存在则尝试创建 读取文件:fread:fread();
readfile(filename):读取文件内容,并把它写入输出缓冲

<?php
echo readfile("webdictionary.txt");
?>
Copy after login
fopen(filename,mode):打开文件,创建文件
<?php
$myfile = fopen("webdictionary.txt", "r") or die("Unable to open file!");
echo fread($myfile,filesize("webdictionary.txt"));
fclose($myfile);
?>
Copy after login
fopen也用于创建文件,如果打开的不存在的文件,此函数会创建文件,嘉定文件被打开为写入(w)或者增加(a)。
Copy after login
模式 描述
r 打开文件为只读。文件指针在文件的开头开始。
w 打开文件为只写。删除文件的内容或创建一个新的文件,如果它不存在。文件指针在文件的开头开始。
a 打开文件为只写。文件中的现有数据会被保留。文件指针在文件结尾开始。创建新的文件,如果文件不存在。
x 创建新文件为只写。返回 FALSE 和错误,如果文件已存在。
r+ 打开文件为读/写、文件指针在文件开头开始。
w+ 打开文件为读/写。删除文件内容或创建新文件,如果它不存在。文件指针在文件开头开始。
a+ 打开文件为读/写。文件中已有的数据会被保留。文件指针在文件结尾开始。创建新文件,如果它不存在。
x+ 创建新文件为读/写。返回 FALSE 和错误,如果文件已存在。
fwrite(资源,内容) //写入文件内容
<?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);
?>
Copy after login
fread():函数读取打开的文件。
fread($myfile,filesize("webdictionary.txt"));
Copy after login

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);
?>
Copy after login
fgetc():读取单个字符
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);
?>
Copy after login

filesize(filename):filename为文件名,是字符串类型。本函数的结果会被缓存,清试用clearstatcache()来清除缓存。 filetype();



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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Chat Commands and How to Use Them
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

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 solve PHP Warning: fopen(): SSL operation failed in file.php on line X How to solve PHP Warning: fopen(): SSL operation failed in file.php on line X Aug 25, 2023 am 09:22 AM

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 PHP Warning: fopen(): failed to open stream: No such file or directory How to solve PHP Warning: fopen(): failed to open stream: No such file or directory Aug 19, 2023 am 10:44 AM

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"

What to do if php+fread() is garbled What to do if php+fread() is garbled Jan 18, 2023 am 10:21 AM

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 PHP Warning: fopen(): failed to open stream: Permission denied How to solve PHP Warning: fopen(): failed to open stream: Permission denied Aug 20, 2023 pm 01:45 PM

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

Usage of fopen function in Matlab Usage of fopen function in Matlab Nov 28, 2023 am 11:03 AM

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 Use java's String.length() function to get the length of a string Jul 25, 2023 am 09:09 AM

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

fread() function in PHP fread() function in PHP Sep 07, 2023 pm 11:57 PM

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

How to use fopen, fwrite and fclose in php for file operations? How to use fopen, fwrite and fclose in php for file operations? Jun 01, 2023 am 08:46 AM

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

See all articles