PHP Chinese website shows you how to operate php files
* Basic process of file operation
* 1. Open the file
* 2. Operate the file: read, write, append, etc.
* 3. Close the file
//1. Create or open a local file
//Open the file in r (read-only) mode, no new file will be created, similar to: r (read-write), the pointer is at the beginning
// $fh = fopen('file1.txt', 'r') or die("Cannot open file1.txt file");
//Open in w (write-only) mode File, if the file does not exist, create it, similar to: w (read and write), the pointer is at the beginning
$fh = fopen('file2.txt', 'w') or die("不能打开file2.txt文件");
//Open the file in a (append write only) mode, if the file does not exist, create it, similar to: a (Append read and write), the pointer is at the end
$fh = fopen('file3.txt', 'a') or die("不能打开file3.txt文件");
//Note: It is recommended to add b to the read and write mode symbols on Windows machines to enhance compatibility with binary files, such as rb, wb...
//2.Open a remote file
$fh = fopen('http://www.php.cn/course/801.html', 'r');
//3.Read the file to the browser
//Read a line from the file pointer and automatically move it down
// while ($s = fgets($fh)) {
// print $s;
// }
//fgetss( ) can filter out all html tags
// while ($s = fgetss($fh)) {
// print $s;
// }
//4. Read the file into a string:
//file_get_contens($filename) returns a string
// $content = file_get_contents('file.txt' );
//Read the entire page into a string, which is very useful when crawling content from other websites, in conjunction with filtering rules
// $content = file_get_contents('http: //www.php.cn');
// echo 'File size: '.strlen($content).' bytes', '
';
/ / if (strlen($content) > 0) {
// echo $content;
// }
//5. Read the entire file into In the array, use newline characters to split
$arr = file('maxim.txt');
// foreach ($arr as $key => $value) {
// echo 'motto'.($key 1).': '.$value.'
';
// }
// shuffle($arr), randomly shuffle an array, return true/false
// if (shuffle($arr)) {
// echo current($arr); //Display a random motto
// echo $arr[0]; //Display a random motto
// }
echo '
';
//array_rand($arr,$length=1): Randomly remove one or more elements from the array
/ /Take out one and return only the key name, if multiple, return an array of random key names
// echo $arr[array_rand($arr)];
print_r(array_rand($arr,3));//返回三个随机的键名 echo '<hr>';
// Traverse this key name array, Query the corresponding array element value
$kes = array_rand($arr,3); foreach ($kes as $value) { //键名无意义,我们只关心值,即键名 print $arr[$value].'<hr>'; }
//After the file reading and writing is completed, it should be closed in time
fclose($fh);
//After closing the script, the file will also be automatically closed, but manual display is still highly recommended This is a good habit

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



To open a php file on a mobile phone, you need to set up a server environment that can run php on the mobile phone and upload the php file to the server. Then, enter the IP address or domain name of the server, plus the path to the php file, into the browser on your phone to open the php file and view its contents.

How to deal with case errors in PHP file paths and generate corresponding error messages. In the process of developing PHP programs, we often encounter the problem of case errors in file paths. Since Windows and Linux systems handle file path case differently, when the program passes the test using the Windows system in the development environment, it may cause path errors when it is deployed to the Linux server. In order to solve this problem, we can use some methods to deal with the large file path

PHP is a widely used server-side programming language. It has powerful file operation capabilities, and the final modification time of a file is also a common requirement for file operations. Therefore, in this article, we will explore an example of PHP file operation function-how to get the last modification time of a file. Using the filemtime() function PHP provides a built-in function called filemtime(), which returns the last modification timestamp of a file. The timestamp is one from the UNIX epoch January 1970

Steps to open a php file: 1. Select a text editor; 2. Create a new file in the selected text editor and save it as a .php file; 3. Write PHP code in the created PHP file; 4. To run PHP files on your local computer, you need to set up a server environment; 5. After installing the server environment, you need to put the PHP files into the server directory; 6. Once you put the PHP files into the server directory, you can browse server to run it.

What can be included in the php file: 1. The starting tag "<?php" and the ending tag "?>", all PHP code must be written inside this pair of tags; 2. The semicolon ";" is a PHP statement The delimiter also represents the instruction for code execution; 3. Comments, including single-line comments "//", multi-line comments "/* */", and Shell comments "#"; 4. Line breaks, which can Enhance the readability of the code; 5. Code segments (such as functions, etc.).

PHP is a widely used open source programming language that is widely used in web development. In PHP, file operations are one of the very common operations. PHP provides a wealth of file operation functions, which can be used for operations such as reading and writing, creating and deleting files. This article will introduce an example of PHP file operation function: file deletion. In PHP, to delete a file, you can use the unlink() function. This function accepts a string parameter representing the path of the file to be deleted. For example, the following code will delete a file called

How to use PHP to write a simple file management system Preface: With the rapid development of the Internet, we come into contact with more and more various files in our daily lives, and it has become particularly important to manage these files effectively. As a commonly used server-side scripting language, PHP can help us build a simple and efficient file management system. This article will introduce in detail how to use PHP to write a file management system with basic functions and provide specific code examples. 1. Build the basic environment Before starting to write the file management system, we

Tools for opening php files: 1. Notepad++; 2. Sublime Text; 3. Visual Studio Code; 4. Eclipse; 5. XAMPP. Detailed introduction: 1. Notepad++, which is a free text editor that supports multiple programming languages, including PHP. It has functions such as syntax highlighting and code folding, making it easier to read and edit PHP code; 2. Sublime Text , a powerful text editor and more.