Table of Contents
回复讨论(解决方案)
Home Backend Development PHP Tutorial php 文件以流的方式传输

php 文件以流的方式传输

Jun 23, 2016 pm 02:14 PM

文件流 流方式传输

 场景:
    客户端获取服务器上的mp3文件播放,但是因为某些原因客户端无法写文件,也就是不能下载操作。
    现在想法是在php端用 fopen -> fread ->echo 把mp3的buffer读到内存,客户端收到buffer存入内存中,然后播放内存中的声音。

 问题:
    现在是服务器上有一个5143字节的Mp3,但是客户端只收到5042.测试是有某些字符被转义了,在网上找过很多方法,似乎都不行,php才学,有什么方法可以让他不转义 得到正确的文件buffer呢。
    

回复讨论(解决方案)

fopen 使用 'rb' 打开

fopen 使用 'rb' 打开
嗯,试过了的..也不行的

yge.me/ xx.mp3/index.php

index.php
header("Location: http://yge.me/x.mp3");
//MP3的全部URL地址.
//x.mp3为真正的文件
?>

1、用 echo file_get_contents('文件名'); 无需考虑其他问题
2、用 readfile('文件名'); 无需考虑其他问题
3、客户端能否写文件,不是服务端能管的事情

1、用 echo file_get_contents('文件名'); 无需考虑其他问题
2、用 readfile('文件名'); 无需考虑其他问题
3、客户端能否写文件,不是服务端能管的事情


下午试了一下午,我确定echo file_get_contents('文件名')应该也是有问题的。
我对比了十六进制数据 发现0D全都丢失了,也就是换行
我在客户端(C++)刚收到数据就开始打印,OD就都没有,应该是在发过来的时候 给转义了吧。
怎么不让这样的字符串转义呢?

问什么要有 0x0d ?
你不是 MP3 吗?没有 0x0d 不是很正常吗?

问什么要有 0x0d ?
你不是 MP3 吗?没有 0x0d 不是很正常吗?

嗯 这个字符可能在mp3中不代表换行吧。因为好像没有这些字符 mp3就是噪音。他只是一个字节流 所以出现0x0D是很正常的吧,毕竟以字节算 也就0-FF这256种。
嗯,我的想法是假设文件大小是5000字节,那么传输5000字节,这样肯定就不会噪音吧。

mp3 是压缩编码的,你要让他不是“噪音”,那就的一段一段的传全了

嗯 是啊。但是0x0D不会传输,被php当成空格去掉  我用pack("c1","13")也显示不了0x0D 导致我的数据不完整了。。

mp3 是压缩编码的,你要让他不是“噪音”,那就的一段一段的传全了 嗯 是啊。但是0x0D不会传输,被php当成空格去掉  我用pack("c1","13")也显示不了0x0D 导致我的数据不完整了。。

不要开玩笑
你的依据何在?

不要开玩笑
你的依据何在?



看图 上为文件原文,下为C++内存调试部分 是不是就是少个0x0D.没有这个的片段都能对上。

php代码就下面三句话
    echo file_get_contents("/var/www/SoundEng/good.mp3");
?>

不要开玩笑
你的依据何在?

然后C++部分是收到原始的数据 肯定不会转义,所以应该是php方面转义了。
我尝试了  echo pack("c1","13"); -- 显示收到的也是00 猜测应该是被转义了,但是苦于对php不熟...

我有个疑问,你的php不会是发了header吧?什么header?

0x0d 的丢失肯定不是 php 端的问题,否则由 php 发出的图片和各种二进制数据不都被破坏了吗?怎么只有你遇到了
我感觉是你的 c 程序的问题,如果你是用 fgets 读取数据的话

我有个疑问,你的php不会是发了header吧?什么header?  试过stream的header muti-byte的 好像都不行

0x0d 的丢失肯定不是 php 端的问题,否则由 php 发出的图片和各种二进制数据不都被破坏了吗?怎么只有你遇到了
我感觉是你的 c 程序的问题,如果你是用 fgets 读取数据的话
恩,我也觉得 是不是要在php的文件里加啥东西啊?代表这是一个流...恩 我不是用fget去读的。我是在C里发出了一个http请求 然后调用系统的API去读,也许这个时候被转义了。好的,谢谢了,我再试试。

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

Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Apr 05, 2025 am 12:04 AM

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,

How does session hijacking work and how can you mitigate it in PHP? How does session hijacking work and how can you mitigate it in PHP? Apr 06, 2025 am 12:02 AM

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.

Describe the SOLID principles and how they apply to PHP development. Describe the SOLID principles and how they apply to PHP development. Apr 03, 2025 am 12:04 AM

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? How to debug CLI mode in PHPStorm? Apr 01, 2025 pm 02:57 PM

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 permissions of unixsocket after system restart? How to automatically set permissions of unixsocket after system restart? Mar 31, 2025 pm 11:54 PM

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

Framework Security Features: Protecting against vulnerabilities. Framework Security Features: Protecting against vulnerabilities. Mar 28, 2025 pm 05:11 PM

Article discusses essential security features in frameworks to protect against vulnerabilities, including input validation, authentication, and regular updates.

Explain late static binding in PHP (static::). Explain late static binding in PHP (static::). Apr 03, 2025 am 12:04 AM

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

See all articles