* 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