Home  >  Article  >  PHP Chinese website shows you how to operate php files

PHP Chinese website shows you how to operate php files

无忌哥哥
无忌哥哥Original
2018-06-28 10:58:501788browse

* 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', '0c6dc11e160d3b678d68754cc175188a';

/ / 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 '0bbd5cc33b622ada7b9ba1b438e60276motto'.($key 1).': 54bdf357c58b8a65c66d7c19c8e4d114'.$value.'f32b48428a809b51f04d3228cdf461fa';

// }

// 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 'f32b48428a809b51f04d3228cdf461fa';

//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;;

// 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;;
}

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

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