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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 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)

11 Best PHP URL Shortener Scripts (Free and Premium) 11 Best PHP URL Shortener Scripts (Free and Premium) Mar 03, 2025 am 10:49 AM

Long URLs, often cluttered with keywords and tracking parameters, can deter visitors. A URL shortening script offers a solution, creating concise links ideal for social media and other platforms. These scripts are valuable for individual websites a

Working with Flash Session Data in Laravel Working with Flash Session Data in Laravel Mar 12, 2025 pm 05:08 PM

Laravel simplifies handling temporary session data using its intuitive flash methods. This is perfect for displaying brief messages, alerts, or notifications within your application. Data persists only for the subsequent request by default: $request-

Build a React App With a Laravel Back End: Part 2, React Build a React App With a Laravel Back End: Part 2, React Mar 04, 2025 am 09:33 AM

This is the second and final part of the series on building a React application with a Laravel back-end. In the first part of the series, we created a RESTful API using Laravel for a basic product-listing application. In this tutorial, we will be dev

Simplified HTTP Response Mocking in Laravel Tests Simplified HTTP Response Mocking in Laravel Tests Mar 12, 2025 pm 05:09 PM

Laravel provides concise HTTP response simulation syntax, simplifying HTTP interaction testing. This approach significantly reduces code redundancy while making your test simulation more intuitive. The basic implementation provides a variety of response type shortcuts: use Illuminate\Support\Facades\Http; Http::fake([ 'google.com' => 'Hello World', 'github.com' => ['foo' => 'bar'], 'forge.laravel.com' =>

cURL in PHP: How to Use the PHP cURL Extension in REST APIs cURL in PHP: How to Use the PHP cURL Extension in REST APIs Mar 14, 2025 am 11:42 AM

The PHP Client URL (cURL) extension is a powerful tool for developers, enabling seamless interaction with remote servers and REST APIs. By leveraging libcurl, a well-respected multi-protocol file transfer library, PHP cURL facilitates efficient execution of various network protocols, including HTTP, HTTPS, and FTP. This extension offers granular control over HTTP requests, supports multiple concurrent operations, and provides built-in security features.

12 Best PHP Chat Scripts on CodeCanyon 12 Best PHP Chat Scripts on CodeCanyon Mar 13, 2025 pm 12:08 PM

Do you want to provide real-time, instant solutions to your customers' most pressing problems? Live chat lets you have real-time conversations with customers and resolve their problems instantly. It allows you to provide faster service to your custom

Announcement of 2025 PHP Situation Survey Announcement of 2025 PHP Situation Survey Mar 03, 2025 pm 04:20 PM

The 2025 PHP Landscape Survey investigates current PHP development trends. It explores framework usage, deployment methods, and challenges, aiming to provide insights for developers and businesses. The survey anticipates growth in modern PHP versio

Notifications in Laravel Notifications in Laravel Mar 04, 2025 am 09:22 AM

In this article, we're going to explore the notification system in the Laravel web framework. The notification system in Laravel allows you to send notifications to users over different channels. Today, we'll discuss how you can send notifications ov

See all articles