file_get_contents读取xls一片乱码?
我在网上下了一个xls,本地wps打开是正常的,但是用file_get_contents或者file()却得到一篇乱码。$S = iconv("utf-8","gb2312", $data); 或$S =iconv("gb2312","utf-8", $data);均无任何效果
请教怎么处理啊。。
谢谢!!
回复讨论(解决方案)
那是当然的!
xls 是二进制文件,你当做文本处理,不出问题才怪呢
那是当然的!
xls 是二进制文件,你当做文本处理,不出问题才怪呢
谢谢版主,我的php代码是下载一个xls文件,我在调试时就发现是乱码,原来是二进制。
我原本希望下载xls文件后处理一下,再保存起来。但是下载下来是乱码,不知道怎么样处理比较好?xls文件近2M多,比较大。
直接用fopen读取吗?
php 处理 xls 文件可以用 PHPExcel 类库
php 处理 xls 文件可以用 PHPExcel 类库
谢谢,请问能不能通过一个函数直接把当前的二进制流转为字符串?
因为我当前下载的数据就是xls 二进制的,文件也很大,如果按前面说的,我必须先保存为xls文件,然后再读取这个xls文件,显得麻烦。
不知道有没有办法可以把当前从网上下载的二进制$file --> $str转为字符串形式?
转成字符串也是乱码,你看不懂的。
转成字符串也是乱码,你看不懂的。
php 处理 xls 文件可以用 PHPExcel 类库
谢谢两位,但PHPExcel感觉真不好用啊。主要是速度实在是慢。或者我刚接触,还在继续研究,是现在就是拿着PHPExcel的例子里直接换成我自己的xls,3分钟过去了,还没读完,xls文档2M,大概6万行。
我把我现在的问题再描述一下:
1、我先在网上curl 下载了一个xls文件,此时尚未保存,因为这个xls文件我是要处理的,所以我希望在还没有保存之前,就把里面数据处理了。但是debug下,xls文件全是乱码。
所以我想问有没有什么办法在此时把这个二进制的数据直接还原成文本,以便于数据处理。
2、假定上面的想法无法实现,那么我只有那curl 下载的数据先保证成一个xxx.xls文件格式,然后再用PHPExcel来打开、读取这个xls文件数据,但是现在的情况,我是一边发帖一边在用PHPExcel读取xls,现在5分钟过去了,仍然没有读完。
请教各位高手,有什么好办法解决?
毕竟那个2m的文档,我file()或是file_get_contents 只是一瞬间就读完了,现在的情况即使PHPExcel最终读取出来了,但如此缓慢的进度已经让数据处理失去意义。。。
建议手动转换成CSV文件,然后再编写代码进行处理,绝对比直接处理XLS文件要快得多。
PHPEXCEL处理小文件还行,处理大文件可别指望它能有多强,虽然提供了几种缓存机制,但是对于大文件基本上是形同虚设。
建议手动转换成CSV文件,然后再编写代码进行处理,绝对比直接处理XLS文件要快得多。
PHPEXCEL处理小文件还行,处理大文件可别指望它能有多强,虽然提供了几种缓存机制,但是对于大文件基本上是形同虚设。
谢谢!但我程序要求采集--> 数据处理 --> 插入数据库 这些全部自动完成,其中采集那一步就看到二进制乱码卡住了....
建议手动转换成CSV文件,然后再编写代码进行处理,绝对比直接处理XLS文件要快得多。
PHPEXCEL处理小文件还行,处理大文件可别指望它能有多强,虽然提供了几种缓存机制,但是对于大文件基本上是形同虚设。
谢谢!但我程序要求采集--> 数据处理 --> 插入数据库 这些全部自动完成,其中采集那一步就看到二进制乱码卡住了....
不明白你说的采集是做什么。仍然建议转换成CSV格式,CSV其实就是固定格式的记录型文本文件。
另外,如果你不适用第三方库去读取EXCEL,那么肯定你读出来的是乱码。流文件和记录型文件不一样。
但转换成CSV之后,就随便你怎么读了。
另外,如果你不适用第三方库去读取EXCEL,那么肯定你读出来的是乱码。流文件和记录型文件不一样。
但转换成CSV之后,就随便你怎么读了。
谢谢指点,因为问题很急,我暂时换了种思路解决。
我说的采集,是说这个xls文件是有php直接网上获取xls数据,然后直接处理数据
(人工无法干预),并不是下载到本地电脑人工转换成csv,除非php有xls -> csv的转换代码,我网上找了,好像不好找。
另外,如果你不适用第三方库去读取EXCEL,那么肯定你读出来的是乱码。流文件和记录型文件不一样。
但转换成CSV之后,就随便你怎么读了。
谢谢指点,因为问题很急,我暂时换了种思路解决。
我说的采集,是说这个xls文件是有php直接网上获取xls数据,然后直接处理数据
(人工无法干预),并不是下载到本地电脑人工转换成csv,除非php有xls -> csv的转换代码,我网上找了,好像不好找。
借助第三方工具转换XLS-CSV也可以。找个其他语言写转换工具,用PHP调用也可以。
PHP Excel类,很多的,很简单,很好用
另外,如果你不适用第三方库去读取EXCEL,那么肯定你读出来的是乱码。流文件和记录型文件不一样。
但转换成CSV之后,就随便你怎么读了。
谢谢指点,因为问题很急,我暂时换了种思路解决。
我说的采集,是说这个xls文件是有php直接网上获取xls数据,然后直接处理数据
(人工无法干预),并不是下载到本地电脑人工转换成csv,除非php有xls -> csv的转换代码,我网上找了,好像不好找。
借助第三方工具转换XLS-CSV也可以。找个其他语言写转换工具,用PHP调用也可以。
谢谢,因为当时处理这个事情很急,所以回避了这个难题,我避开了读取xls换一种思路解决了。这几天在研究你说的php xls转换

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

Alipay PHP...

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

Session hijacking can be achieved through the following steps: 1. Obtain the session ID, 2. Use the session ID, 3. Keep the session active. The methods to prevent session hijacking in PHP include: 1. Use the session_regenerate_id() function to regenerate the session ID, 2. Store session data through the database, 3. Ensure that all session data is transmitted through HTTPS.

The enumeration function in PHP8.1 enhances the clarity and type safety of the code by defining named constants. 1) Enumerations can be integers, strings or objects, improving code readability and type safety. 2) Enumeration is based on class and supports object-oriented features such as traversal and reflection. 3) Enumeration can be used for comparison and assignment to ensure type safety. 4) Enumeration supports adding methods to implement complex logic. 5) Strict type checking and error handling can avoid common errors. 6) Enumeration reduces magic value and improves maintainability, but pay attention to performance optimization.

The application of SOLID principle in PHP development includes: 1. Single responsibility principle (SRP): Each class is responsible for only one function. 2. Open and close principle (OCP): Changes are achieved through extension rather than modification. 3. Lisch's Substitution Principle (LSP): Subclasses can replace base classes without affecting program accuracy. 4. Interface isolation principle (ISP): Use fine-grained interfaces to avoid dependencies and unused methods. 5. Dependency inversion principle (DIP): High and low-level modules rely on abstraction and are implemented through dependency injection.

How to debug CLI mode in PHPStorm? When developing with PHPStorm, sometimes we need to debug PHP in command line interface (CLI) mode...

How to automatically set the permissions of unixsocket after the system restarts. Every time the system restarts, we need to execute the following command to modify the permissions of unixsocket: sudo...

Sending JSON data using PHP's cURL library In PHP development, it is often necessary to interact with external APIs. One of the common ways is to use cURL library to send POST�...
