-
-
//Read file - $file_path="text.txt";
if(!file_exists($file_path)){
- echo "File does not exist";
- exit();
- }
-
- //Open the file
- $fp=fopen($file_path,"a+");
- //Read the file
- $content=fread($fp, filesize($file_path));
- echo "The content of the file is:
";
- //By default, after the content is output to the web page, it will not be displayed in a new line because the web page does not recognize rn
- //All required Put rn =>
-
- $content=str_replace("rn","
",$content);
- echo $content;
fclose($fp);
- ?>
-
Copy code
2. The second way to read files
-
-
- //The second way to read files
$file_path="text.txt";
- if(!file_exists ($file_path)){
- echo "File does not exist";
- exit();
- }
- $content=file_get_contents($file_path);
$content=str_replace("rn", " ",$content);
- echo $content;
- ?>
-
Copy code
3. How to read in a loop (to deal with large files)
-
-
- //The third reading method, loop reading (for large files)
$file_path="text.txt ";
- if(!file_exists($file_path)){
- echo "The file does not exist";
- exit();
- }
//Open the file
- $fp=fopen($file_path ,"a+");
- //Define how many bytes to read each time
- $buffer=1024;
- //Read simultaneously. Determine whether the end of the file is reached while(!feof($fp)){
- //Read data in 1024 bytes
- $content=fread($fp,$buffer);
- echo $content;
- }< /p>
fclose($fp);
- ?>
-
-
Copy the code
4. Read the ini configuration file
1), db.ini file
-
$arr=parse_ini_file("db.ini"); - echo "
";
- print_r($arr);
- echo " pre>";
-
- echo $arr['host'];
//Connect to the database
- $conn = mysql_connect($arr['host'], $arr['user'] , $arr['pwd']);
if(!$conn){
- echo "error";
- }
echo "OK";
- ?>
-
-
Copy code
|