search
HomeBackend DevelopmentPHP ProblemHow 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
  • $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

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);
?>
输出结果:php.cn goo

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);
?>
输出结果:php.cn good better

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

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool