Current Location: Home> Function Categories> fgets

fgets

Read a line from a file pointer
Name:fgets
Category:File system
Programming Language:php
One-line Description:Return a line from the opened file.

实例

例子 1

<span class="token php language-php"><span class="token delimiter important"><?php</span>

<span class="token variable">$file</span> <span class="token operator">=</span> <span class="token function">fopen</span><span class="token punctuation">(</span><span class="token string double-quoted-string">"test.txt"</span><span class="token punctuation">,</span><span class="token string double-quoted-string">"r"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">echo</span> <span class="token function">fgets</span><span class="token punctuation">(</span><span class="token variable">$file</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token function">fclose</span><span class="token punctuation">(</span><span class="token variable">$file</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

<span class="token delimiter important">?></span></span>

输出类似:

Hello, this is a test file.

例子 2

<span class="token php language-php"><span class="token delimiter important"><?php</span>

<span class="token variable">$file</span> <span class="token operator">=</span> <span class="token function">fopen</span><span class="token punctuation">(</span><span class="token string double-quoted-string">"test.txt"</span><span class="token punctuation">,</span><span class="token string double-quoted-string">"r"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

<span class="token keyword">while</span><span class="token punctuation">(</span><span class="token operator">!</span> <span class="token function">feof</span><span class="token punctuation">(</span><span class="token variable">$file</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
  <span class="token punctuation">{</span>
  <span class="token keyword">echo</span> <span class="token function">fgets</span><span class="token punctuation">(</span><span class="token variable">$file</span><span class="token punctuation">)</span><span class="token operator">.</span> <span class="token string double-quoted-string">"<br />"</span><span class="token punctuation">;</span>
  <span class="token punctuation">}</span>

<span class="token function">fclose</span><span class="token punctuation">(</span><span class="token variable">$file</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token delimiter important">?></span></span>

输出类似:

Hello, this is a test file. 
There are three lines here. 
This is the last line.
Similar Functions
  • Create a new directory mkdir

    mkdir

    Createanewdirectory
  • Determine whether the given file name is a directory is_dir

    is_dir

    Determinewhethertheg
  • Read a line from a file pointer fgets

    fgets

    Readalinefromafilepo
  • Get file modification time filemtime

    filemtime

    Getfilemodificationt
  • Test whether the file pointer reaches the end of the file feof

    feof

    Testwhetherthefilepo
  • Returns the total size of the file system or disk partition disk_total_space

    disk_total_space

    Returnsthetotalsizeo
  • Returns the file path information pathinfo

    pathinfo

    Returnsthefilepathin
  • Read files (safely used in binary files) fread

    fread

    Readfiles(safelyused
Popular Articles