---
title: "PHP 读取 txt 文件"
author: "onestopweb"
date: "2018-01-14T11:01:04Z"
tags: []
categories: ["编程语言"]
slug: "php-txt"
draft: false
lastmod: "2018-01-14T11:01:04Z"
id: 2407691
url: "https://onestopweb.iteye.com/blog/2407691"
---

```
<?php
header('Content-Type: text/html; charset=utf-8');

$file = fopen("test.txt", "r");
$contArr=array();
$i=0;
//输出文本中所有的行，直到文件结束为止。
while(! feof($file)) {
    $contArr[$i]= fgets($file);//fgets()函数从文件指针中读取一行
    $i++;
}
fclose($file);
$contArr=array_filter($contArr);
var_dump($contArr);
```

 

效果图：  
![](http://dl2.iteye.com/upload/attachment/0128/5377/e716a9de-e7bc-3851-b004-4f30701653db.jpg)