Home >Backend Development >PHP Tutorial >linux - php processing image download error
1 When the user clicks on the download QR code link on the website, the server obtains the QR code generated by WeChat and then downloads it to the user's computer. The same code test machine works fine, but the online image cannot be opened.
The code is as follows
<code> public function download(){ $url = "https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket=xxxxxxxxxx"; $tmp = file_get_contents($url); $file_name = "weixin.jpg"; // 输入文件标签 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(); }</code>It has been basically determined to be caused by different environments.
Are there any requirements for server configuration or installed software for these few lines of code to download images?
---------------------Divider line------------------------
passed The detection found that there are blank lines in the output of other classes referenced online (it may be the BOM header, or it may be the closing character of the PHP code?> There are blank lines after the header), resulting in output before the header, the picture cannot be opened, and the file is too large There are so many, so hard to find.
After adding the following code, it is solved.
<code>//清除之前的输出,确保图片完整。 ob_start(); ob_end_flush(); ob_end_clean();</code>Reply content:
<code> public function download(){ $url = "https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket=xxxxxxxxxx"; $tmp = file_get_contents($url); $file_name = "weixin.jpg"; // 输入文件标签 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(); }</code>
With the same code, the downloaded pictures can be opened on the test machine, but cannot be opened on the online machine
passed The detection found that there are blank lines in the output of other classes referenced online (it may be the BOM header, or it may be the closing character of the PHP code? > There are blank lines after the header), resulting in output before the header, the picture cannot be opened, and the file is too large There are so many, so hard to find.
After adding the following code, it is solved.
<code>//清除之前的输出,确保图片完整。 ob_start(); ob_end_flush(); ob_end_clean();</code>
. Your URL is https, and
file_get_contents will get stuck. Try using curl instead of
file_get_contents which should solve the problem.
(There are two SSL settings in curl that allow the program to access https normally. By the way, everyone should know this)
<code> Header("Accept-Length: ".strlen($tmp)); 不定义这个试试</code>
strlen
When encountering a 0
, it will be considered that the end of the string has been reached, and
0 may appear in the middle of the picture, so only part of the data of the picture is obtained, and of course it cannot be opened.
For example
<pre class="brush:php;toolbar:false"><code>$hex='3332310033323131313131313131313131313131313131313131313131313131313131313131313131'
//结果是321,即0后面的都无效。</code></pre>