Error in image download processing in linux-php

Source: Internet
Author: User
1. when a user clicks the QR code download link on the website, the server obtains the generated QR code and downloads it to the same code test machine on the user's computer. the image cannot be opened online, the code is as follows {code ...} it is basically determined that the environment is different. Download the image code. when you click download QR code link on the website, the server obtains the generated QR code and downloads it to the user's computer.
There is no problem with the same code testing machine. the image cannot be opened online,
The code is as follows:

Public function download () {$ url = "https://mp.weixin.qq.com/cgi-bin/showqrcode? Ticket = xxxxxxxxxx "; $ tmp = file_get_contents ($ url); $ file_name =" weixin.jpg "; // enter the file tag Header (" Content-type: application/octet-stream "); Header (" Accept-Ranges: bytes "); Header (" Accept-Length :". strlen ($ tmp); Header ("Content-Disposition: attachment; filename = ". $ file_name); // echo $ tmp; exit ();}

It is basically determined that the environment is different.
What are the requirements for server configuration or software installation?

In the same code, the downloaded images can be opened on the testing machine. The Online machines cannot be opened.

--------------------- Separation line ------------------------
After detection, it is found that other classes referenced online have blank row output (which may be the bom header or the php code closure?> It is too difficult to find the output header, the image cannot be opened, and there are too many files.
Add the following code to solve the problem.

// Clear the previous output to ensure the image is complete. Ob_start (); ob_end_flush (); ob_end_clean ();

Reply content:

1. when a user clicks the download QR code link of the website, the server obtains the generated QR code and downloads it to the user's computer.
There is no problem with the same code testing machine. the image cannot be opened online,
The code is as follows:

Public function download () {$ url = "https://mp.weixin.qq.com/cgi-bin/showqrcode? Ticket = xxxxxxxxxx "; $ tmp = file_get_contents ($ url); $ file_name =" weixin.jpg "; // enter the file tag Header (" Content-type: application/octet-stream "); Header (" Accept-Ranges: bytes "); Header (" Accept-Length :". strlen ($ tmp); Header ("Content-Disposition: attachment; filename = ". $ file_name); // echo $ tmp; exit ();}

It is basically determined that the environment is different.
What are the requirements for server configuration or software installation?

In the same code, the downloaded images can be opened on the testing machine. The Online machines cannot be opened.

--------------------- Separation line ------------------------
After detection, it is found that other classes referenced online have blank row output (which may be the bom header or the php code closure?> It is too difficult to find the output header, the image cannot be opened, and there are too many files.
Add the following code to solve the problem.

// Clear the previous output to ensure the image is complete. Ob_start (); ob_end_flush (); ob_end_clean ();

I have encountered this problem. I think the error isfile_get_contents. Your url is https,file_get_contentsThe result will be stuck. try curl instead.file_get_contentsThe problem should be solved.
(There are two ssl Settings in curl that allow the program to access https normally. This should be known as this)

Header ("Accept-Length:". strlen ($ tmp); do not define this try

strlen
When0It is considered that the string has reached the end, and the middle of the image may appear0So I only obtained part of the image data. of course, I cannot open it.
For example

$ Hex = '000000' // The result is 3332310033323131313131313131313131313131313131313131313131313131313131313131313131, that is, all values after 0 are invalid.

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.