Home Backend Development PHP Tutorial 关于PHP使用fread读取文件总是会多一个空字符的有关问题

关于PHP使用fread读取文件总是会多一个空字符的有关问题

Jun 13, 2016 pm 01:26 PM
filepath filesize fread

关于PHP使用fread读取文件总是会多一个空字符的问题
代码如下:

PHP code
<!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

-->
$fileSize = filesize($filePath);
$handle = fopen($filePath, "rb");


while (!feof($handle)) {
        var_dump(fread($handle, $fileSize));
        //会多输出一次空字符
}


Copy after login



求教,谢谢!

------解决方案--------------------
fread($handle, $fileSize)
文件中正好有 $fileSize 字节的内容,读取顺利,因此程序并没有检测到文件已到底。那么就会执行第二次循环,而此时指针位于最后一位,再没有什么可读的了,因此系统才将文件指针设为触底,这样feof()才会返回true。php操作文件系统和C几乎没什么区别,这方面有关于C的feof()有很详细的讲解
------解决方案--------------------
其实我也并没有说到点子上.至于解决方法,可以对 $fileSize+1.或者你完全可以不用循环 fread($handle, filesize ($filename)); 即可读取全部文件内容。
------解决方案--------------------
trim() 一下?
------解决方案--------------------
如果文件不大,就全读到数组,去掉最后一个吧。如果文件较大,另想其法了,不过没有发现多了这个会影响什么呀。真的影响了,想去掉应该也不是什么难事。
------解决方案--------------------
因为你是WINDOWS平台,文件是文本打开存储的内容,末尾会有一个特殊字节标识文件结束,你用rb打开自然就可以读到最后那个特殊字节了。

用r打开,fgets读吧。
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 Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

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)

Hot Topics

Java Tutorial
1669
14
PHP Tutorial
1273
29
C# Tutorial
1256
24
PHP Warning: filesize() [function.filesize]: stat failed solution PHP Warning: filesize() [function.filesize]: stat failed solution Jun 22, 2023 pm 11:54 PM

When developing PHP projects, we often encounter problems related to file operations. One of the problems that often occurs is the error prompt "PHPWarning: filesize()[function.filesize]:statfailed". This error message often makes people confused and it is difficult to find a solution. This article will introduce the cause and solution of this problem, hoping to help everyone. The cause of the problem is in PHP, filesize

What to do if php+fread() is garbled What to do if php+fread() is garbled Jan 18, 2023 am 10:21 AM

The garbled code in php+fread() is because the encoding of the output page is inconsistent with the encoding of the read file. The solution: 1. Open the corresponding PHP file; 2. Read the file through the fread function; 3. Through "iconv('gbk' , 'utf-8', $data)" method to transcode the read content.

fread() function in PHP fread() function in PHP Sep 07, 2023 pm 11:57 PM

The fread() function reads data from an open file. The fread() function stops at the end of the file or when it reaches the specified length. Returns the read string on success. Returns FALSE on failure. Syntax fread(file_pointer,length) Parameter file_pointer−The file system pointer resource created using fopen(). Required. length−Maximum number of bytes to read. Required. Return Value If successful, the fread() function returns the read string. On failure, returns FALSE. Suppose we have a file called "one.txt" where

How to solve 'undefined: filepath.Glob' error in golang? How to solve 'undefined: filepath.Glob' error in golang? Jun 24, 2023 pm 04:00 PM

Go language is a lightweight programming language that is widely used to build high-performance network services. In Go application development, we usually need to use file path operations. In Go language, file path operations are supported by the filepath package provided by the standard library. The filepath package provides some functions and constants for operating file paths. One of these functions is Glob(), which searches for paths to files matching a pattern. However, when using the Go language, you may encounter "undefine

How to solve 'undefined: filepath.Abs' error in golang? How to solve 'undefined: filepath.Abs' error in golang? Jun 24, 2023 pm 04:37 PM

In golang, filepath.Abs ​​is a very commonly used library. Its function is to obtain the absolute path of a relative path. However, in some cases, we will find that we will encounter an "undefined: filepath.Abs" error when using filepath.Abs, which means that we did not introduce the filepath library correctly or wrote the wrong syntax. This article explains how to resolve this error. First, we need to understand

PHP function introduction: is_file() function PHP function introduction: is_file() function Nov 04, 2023 am 09:11 AM

PHP function introduction: is_file() function In PHP programming, the is_file() function is a very useful function. It is used to determine whether a path or file exists and is an ordinary file. In this article, we will introduce how to use the is_file() function and provide some specific code examples. First, let's take a look at the syntax of the is_file() function: boolis_file(string$filename)is_

PHP function introduction—fread(): Read content of a specified length from a file PHP function introduction—fread(): Read content of a specified length from a file Jul 26, 2023 pm 09:29 PM

Introduction to PHP functions—fread(): Read content of a specified length from a file. PHP is a widely used scripting language for developing web applications. In PHP, there are many built-in functions for processing files, one of which is the fread() function. The fread() function allows us to read a specified length of content from an open file. Syntax: stringfread(resource$handle,int$length) Parameter description: $h

PHP returns the total disk size of a directory PHP returns the total disk size of a directory Mar 21, 2024 pm 10:30 PM

This article will explain in detail about the total disk size of a directory returned by PHP. The editor thinks it is quite practical, so I share it with you as a reference. I hope you can gain something after reading this article. Calculate total directory disk size in PHP Background In some cases, it is necessary to determine the amount of disk space occupied by a specific directory or its subdirectories. PHP provides powerful functions to perform this task, allowing developers to easily calculate the total disk size of a directory. Method 1. Use the disk_total_space() function. The disk_total_space() function obtains the total disk capacity of a given directory or file. $directory=&quot;/var/log&quot;;$total_space=

See all articles