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

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics










![PHP Warning: filesize() [function.filesize]: stat failed solution](https://img.php.cn/upload/article/000/887/227/168744929486784.jpg?x-oss-process=image/resize,m_fill,h_207,w_330)
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

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.

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

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

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

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

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="/var/log";$total_space=
