Table of Contents
PHP 创建文件 - fopen()
实例
PHP 文件权限
PHP 写入文件 - fwrite()
PHP 覆盖(Overwriting)
PHP 增加(add)
Home Backend Development PHP Tutorial PHP 文件创建/写下

PHP 文件创建/写下

Jun 13, 2016 pm 12:24 PM
fopen fwrite txt

PHP 文件创建/写入

PHP 创建文件 - fopen()

fopen() 函数也用于创建文件。也许有点混乱,但是在 PHP 中,创建文件所用的函数与打开文件的相同。

如果您用 fopen() 打开并不存在的文件,此函数会创建文件,假定文件被打开为写入(w)或增加(a)。

下面的例子创建名为 "testfile.txt" 的新文件。此文件将被创建于 PHP 代码所在的相同目录中:

实例

$myfile = fopen("testfile.txt", "w")
Copy after login

PHP 文件权限

如果您试图运行这段代码时发生错误,请检查您是否有向硬盘写入信息的 PHP 文件访问权限。

PHP 写入文件 - fwrite()

fwrite() 函数用于写入文件。

fwrite() 的第一个参数包含要写入的文件的文件名,第二个参数是被写的字符串。

下面的例子把姓名写入名为 "newfile.txt" 的新文件中:

实例

<?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

请注意,我们向文件 "newfile.txt" 写了两次。在每次我们向文件写入时,在我们发送的字符串 $txt 中,第一次包含 "Bill Gates",第二次包含 "Steve Jobs"。在写入完成后,我们使用 fclose() 函数来关闭文件。

如果我们打开 "newfile.txt" 文件,它应该是这样的:

Bill GatesSteve Jobs
Copy after login

PHP 覆盖(Overwriting)

如果现在 "newfile.txt" 包含了一些数据,我们可以展示在写入已有文件时发生的的事情。所有已存在的数据会被擦除并以一个新文件开始。

在下面的例子中,我们打开一个已存在的文件 "newfile.txt",并向其中写入了一些新数据:

实例

<?php $myfile = fopen("newfile.txt", "w") or die("Unable to open file!");$txt = "Mickey Mouse\n";fwrite($myfile, $txt);$txt = "Minnie Mouse\n";fwrite($myfile, $txt);fclose($myfile);?>
Copy after login

如果现在我们打开这个 "newfile.txt" 文件,Bill 和 Steve 都已消失,只剩下我们刚写入的数据:

Mickey MouseMinnie Mouse
Copy after login

PHP 增加(add)

如果希望在文件中持续添加但不覆盖,使用如下格式

<?php $myfile = fopen("newfile.txt", "a");$txt = "Mickey Mouse\n";fwrite($myfile, $txt);$txt = "Minnie Mouse\n";fwrite($myfile, $txt);fclose($myfile);?>
Copy after login

使用"a",为add增加的意思,操作文件时只做添加操作。

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)

What is Intel TXT? What is Intel TXT? Jun 11, 2023 pm 06:57 PM

IntelTXT is a hardware-assisted security technology launched by Intel. It can ensure the integrity and security of the server during startup by establishing a protected space between the CPU and BIOS. The full name of TXT is TrustedExecutionTechnology, which is Trusted Execution Technology. Simply put, TXT is a security technology that provides hardware-level protection to ensure that the server has not been modified by malicious programs or unauthorized software when it is started. this one

How to convert html to txt How to convert html to txt Aug 31, 2023 am 09:23 AM

Methods for converting html to txt include using a text editor, using online conversion tools, and using Python programming. Detailed introduction: 1. To open an HTML file, you can use any text editor, such as Notepad, Sublime Text, etc. To select the content of the entire HTML file, you can press the Ctrl+A shortcut key or drag the mouse to select and copy the selection. The content can be copied by pressing the Ctrl+C shortcut or through the copy option in the right-click menu, opening a new TXT file, using the same text editor, etc.

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: 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

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"

How to convert chm to txt How to convert chm to txt Oct 17, 2023 pm 02:42 PM

chm is converted to txt by using online conversion tools, using browser plug-ins, using command line tools and using third-party software. Detailed introduction: 1. Use the online conversion tool, just upload the CHM file, select the TXT format, and then download the converted TXT file; 2. Use the browser plug-in, after installing the plug-in, just open the CHM file in the browser, and then Click the plug-in button to convert CHM files into TXT format; 3. Use command line tools, etc.

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.

FAQ for pandas reading txt files FAQ for pandas reading txt files Jan 19, 2024 am 09:19 AM

Pandas is a data analysis tool for Python, especially suitable for cleaning, processing and analyzing data. During the data analysis process, we often need to read data files in various formats, such as Txt files. However, some problems will be encountered during the specific operation. This article will introduce answers to common questions about reading txt files with pandas and provide corresponding code examples. Question 1: How to read txt file? txt files can be read using the read_csv() function of pandas. This is because

See all articles