Home > headlines > body text

PHP Chinese website shows you how to operate php files

无忌哥哥
Release: 2018-06-28 10:58:50
Original
1849 people have browsed it

* 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文件");
Copy after login

//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文件");
Copy after login

//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');
Copy after login

//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 &#39;<hr>&#39;;
Copy after login

// Traverse this key name array, Query the corresponding array element value

$kes = array_rand($arr,3);
foreach ($kes as $value) { //键名无意义,我们只关心值,即键名
print $arr[$value].&#39;<hr>&#39;;
}
Copy after login

//After the file reading and writing is completed, it should be closed in time

fclose($fh);
Copy after login

//After closing the script, the file will also be automatically closed, but manual display is still highly recommended This is a good habit

source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template