Get the App
SLTechnology News&Howtos  ›  Development  › 

How to modify the contents of txt files with php

Shulou Source: shulou.com Published: 2022-06-03 05:22:55 11月22日 Update

This article focuses on "how to use php to modify the contents of txt files", interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn how to use php to modify the contents of txt files.

Php modify txt file content method: 1, create a PHP sample file; 2, open the file through fopen; 3, through "fwrite ($fp,$password);" modify the txt file content.

This article operating environment: windows7 system, PHP7.1 version, DELL G3 computer

How does php modify the contents of txt files?

Php reads and modifies txt files

/ / there is only one line of data read in the txt file.

$fp = fopen ("password.txt", "r"); if ($fp) {$pwd = fgets ($fp);} else {echo "failed to open file";} fclose ($fp)

/ / there is only one line of data written in the txt file

$fp = fopen ("password.txt", "w"); / / write to if ($fp) {$flag=fwrite ($fp,$password); if (! $flag) {echo "failed to write to the file

"; break;}} fclose ($fp)

/ / multiple lines of data in the txt file are read in the form of an array

$path= "wifi_customer_settings.txt"; $body = file_get_contents ($path); if (file_exists ($path)) {$body = file_get_contents ($path); / / converted array / / echo "alert ('file exists');";} else {/ / echo "alert ('file does not exist $path');";} $cbody = file ($path); / / print_r ($cbody); $wifissid=$cbody [0]; $wifipwd=$cbody [1] $wifissid=str_replace ('wifiSSID=','', $wifissid); / / text replacement replace the wifiSSID of the value of $Wifissidwith' $wifipwd=str_replace ('wifiPWD=','', $wifipwd)

/ / multiple lines of data are written in an array in the txt file.

$path= "wifi_customer_settings.txt"; $wifissid2=$_POST ['wifissid']; $wifipwd2=$_POST [' wifipwd']; $arr=array ("wifiSSID=". $wifissid2, "\ n", "wifiPWD=". $wifipwd2); $fp=fopen ($path,'w'); fputs ($fp,$arr [0]); fputs ($fp,$arr [1]); fputs ($fp,$arr [2]); fclose ($fp) At this point, I believe you have a deeper understanding of "how to use php to modify the contents of txt files". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Tags: File content data array one line only form method multiline learning practical deeper interest practical practical simple operation text more friends environment Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Information Docker vpn Microsoft Xiaomi