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

AI Hentai Generator
Generate AI Hentai for free.

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.

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

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

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_

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

PHP function introduction: filesize() function overview: In PHP development, we often need to obtain file size information, and PHP provides a convenient function to achieve this purpose - the filesize() function. The filesize() function is used to obtain the size of the specified file, and the return value is the size of the file (in bytes). This article will introduce the usage of filesize() function in detail and provide specific code examples. Usage: How to use the filesize() function

PHP is a programming language widely used in web development and has many file operation functions that can be used to read, write and process files. These functions are very useful because files are a common form of data processing in web development. This article will introduce some commonly used PHP file operation functions and provide some sample code to help you understand better. 1. Open and close files. To use PHP file operation functions, you first need to open the file. The commonly used function is fopen(), which requires two parameters: file name and open
