Php files are transmitted as streams

Source: Internet
Author: User
Php files are transmitted as streams.

Scenario:
The client obtains the mp3 file on the server for playing, but the client cannot write the file or download the file for some reason.
The idea is to use fopen-> fread-> echo on the php end to read the mp3 buffer into the memory. the client receives the buffer and stores it in the memory, and then plays the sound in the memory.

Problem:
Now there is a 5143-byte Mp3 on the server, but the client only receives 5042. some characters have been escaped during the test. I have found many methods on the internet and it seems that I can't even use them. php has just learned how to prevent him from escaping to get the correct file buffer.


Reply to discussion (solution)

Use 'RB' to open fopen

Use 'RB' to open fopen
Well, I tried it.

Yge. me/xx133/index. php

Index. php
Header ("Location: http://yge.me/x.mp3 ");
// All URL addresses of MP3.
// X.mp3 is a real file
?>

1. use echo file_get_contents ('filename '). you do not need to consider other issues.
2. use readfile ('filename '). you do not need to consider other issues.
3. whether the client can write files is not managed by the server.

1. use echo file_get_contents ('filename '). you do not need to consider other issues.
2. use readfile ('filename '). you do not need to consider other issues.
3. whether the client can write files is not managed by the server.


After trying this afternoon, I confirmed that echo file_get_contents ('filename ') should also be faulty.
I compared the hexadecimal data and found that all 0D is lost, that is, line feed.
I started to print the data at the client (C ++), and I didn't even have the OD. it should have been escaped when I sent it.
Why not let such a string escape?

What is 0x0d?
Aren't you MP3? Isn't it normal to have 0X0 d?

What is 0x0d?
Aren't you MP3? Isn't it normal to have 0X0 d?

Well, this character may not represent a line break in mp3. It seems that without these characters mp3 is noise. It is normal to see 0x0D because it is only a byte stream. after all, the 256 bytes are counted as 0-FF.
Well, my idea is that if the file size is 5000 bytes, transfer 5000 bytes so that it will certainly not make noise.

Mp3 files are compressed and encoded. if you want to make them not "noisy", you can upload all of them.

Yeah. However, 0x0D will not be transmitted. if it is replaced by a space in php, pack ("c1", "13") cannot be used to display 0x0D, resulting in incomplete data ..

Mp3 files are compressed and encoded. if you want to make them not "noisy", you can transfer all of them. However, 0x0D will not be transmitted. if it is replaced by a space in php, pack ("c1", "13") cannot be used to display 0x0D, resulting in incomplete data ..

Don't joke
What is your basis?

Don't joke
What is your basis?



The figure shows the original file, and whether the C ++ memory debugging part is less than 0X0 D. no such part can be matched.

Php code:
Echo file_get_contents ("/var/www/SoundEng/good.mp3 ");
?>

Don't joke
What is your basis?

Then, in C ++, the original data is certainly not escaped, so it should be escaped in php.
I tried echo pack ("c1", "13"); -- it shows that I received a 00 guess that it should have been escaped, but I am not familiar with php...

I have a question, isn't your php sending a header? What header?

The loss of 0x0d is definitely not a problem on the php side. otherwise, will the images and binary data sent by php not be damaged? How come you met
I think it's a problem with your c program. if you use fgets to read data

I have a question, isn't your php sending a header? What header? I have tried stream header muti-byte. none of them seem to work.

The loss of 0x0d is definitely not a problem on the php side. otherwise, will the images and binary data sent by php not be damaged? How come you met
I think it's a problem with your c program. if you use fgets to read data
Well, I also think I want to add something to a php file? It indicates that this is a stream... well, I didn't use fget to read it. I sent an http request in C and then called the system API to read it. it may be escaped at this time. Okay. thank you. let me try again.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.