Home Backend Development PHP Problem How to use fread() to read files in PHP

How to use fread() to read files in PHP

Apr 15, 2021 am 09:47 AM
filesize()

How to use fread() to read files in PHP

This article mainly introduces how to use PHP’s built-in fread() function in PHP to obtain the number of bytes when confirming and not confirming the acquisition. Number of bytes, in both cases, the method used to read the file contents.

1.fread syntax:

fread  ( resource $handle  , int $length   ) : string
Copy after login
  • $handle: file system Pointer, usually a resource created by fopen().

  • $length: Read the byte length of the file.

  • Return value: A string of $length length.

2. Read the file (confirm the bytes that need to be read):

exit.txt file

php.cn good better
Copy after login

php file

<?php
  //文件路径
  $filename="./exit.txt";
  //获取文件资源
  $file = fopen($filename,&#39;r&#39;); //读取二进制文件时,需要将第二个参数设置成&#39;rb&#39;
  //获取文件内容
  $file_info=fread($file,10);
  //打印文件内容
  echo $file_info;
  //关闭文件资源
    fclose($file);
?>
Copy after login
输出结果:php.cn goo
Copy after login

3. Read the file (do not confirm the bytes to be read):

If you need to get all the contents of the file, you need to use another function filesize() function, which is specially used to check the size of the file

<?php
  //文件路径
  $filename="./exit.txt";
  //获取文件资源
  $file = fopen($filename,&#39;r&#39;);
  //获取文件大小
  $file_size= filesize($filename);
  //获取文件内容
  $file_info=fread($file, $file_size);
   //打印文件内容
  echo $file_info;
  //关闭文件资源
  fclose($file);
?>
Copy after login
输出结果:php.cn good better
Copy after login

Recommendation: 2021 Summary of annual PHP interview questions (collection)》《php video tutorial

The above is the detailed content of How to use fread() to read files in PHP. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)